On Sun, Nov 29, 2009 at 3:58 PM, John W. Krahn <jwkr...@shaw.ca> wrote:
> raphael() wrote: > >> Hi, >> > > Hello, > > > I want the below if loop to end if it cannot find any match & print the >> die >> message. >> However it just exit without hitting my "die" >> >> As you can see in the code below I have tried many foolish ways to make >> the >> script say >> that it cannot find the number searched in while loop. >> >> while ( my $line = $FH->readline ) { >> chomp; >> > > That should be: > > chomp $line; > > Although from your code it looks like you don't really need to chomp() at > all. > > > > next if ( $line =~ m/return/i ); >> if ( $line =~ m/^$number\s+(\S+)\s+(.*)$/ ) { >> >> &get_info($number, $1, $2); # if ( $1, $2 ); # <-- don't >> > > You shouldn't use an ampersand when you call a subroutine: > > > get_info($number, $1, $2); > > > execute sub &get_info unless a match >> >> die "\nNo Entries Found By Number: [$number]\n" unless ($1); >> > > $1 is captured from the pattern (\S+) so it will *always* be true unless > the string it contains is '0' which is false. > > > > # print $1; exit; >> >> last; >> } >> } >> $FH->close(); >> >> Any Ideas? >> > > > > John > -- > The programmer is fighting against the two most > destructive forces in the universe: entropy and > human stupidity. -- Damian Conway > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > Sorry for insufficient data. Here is the whole subroutine ----- CODE ----- sub who_is_who($) { my $FH = File::ReadBackwards->new( 'INFO_DB.TXT' ); my $number = $_[0]; # print "\$number is $_[0]\n"; while ( my $line = $FH->readline ) { # chomp; next if ( $line =~ m/return/i ); # next line if line has the word return in it if ( $line =~ m/^$number\s+(\S+)\s+(.*)$/ ) { &get_info($number, $1, $2); # if ( $1, $2 ); # die "\nNo Entries Found By Number: [$number]\n" unless ($1); # print $1; exit; last; } } $FH->close(); } ----- CODE ----- INFO_DB.TXT NUM DATE NAME 1111 20.11.2009 SOME_NAME_1 1112 21.11.2009 SOME_NAME_1 1113 20.11.2009 SOME_NAME_2 1114 25.11.2009 SOME_NAME_5 The thing is that if "$number" is not found in the file the subroutine should say that it cannot find the "given number" in the file. $1 is date and $2 is name. If match is successful i.e. number is found then all the info is sent to another subroutine get_info and so on..