in reply to Ending a 'next unless' loop...

If you're reading from a file (well, anything except a terminal), then you could use eof.
if (eof(EMPLOYEES)) { print "Sorry, your query produced no results.\n"; } else { while (my $line = <EMPLOYEES>) { #remove the carriage return chomp $line; #splits the line between tabs and get the different elements ($name, $city, $language) = split /\t/, $line; next unless $language =~ /$search/; print "$name can speak " . ($search) . " and lives in " . ($city) . ".\n"; } }

Replies are listed 'Best First'.
Re^2: Ending a 'next unless' loop...
by grep (Monsignor) on Oct 04, 2006 at 05:18 UTC
    There is one problem I see with this approach.

    Unless I'm missing something I think you meant

    open(EMPLOYEES,'test.txt') or die; while (my $line = <EMPLOYEES>) { #remove the carriage return chomp $line; #splits the line between tabs and get the different elements my ($name, $city, $language) = split /\t/, $line; next unless $language =~ /$search/; print "$name can speak " . ($search) . " and lives in " . ($city) . ".\n"; } print "Sorry, your query produced no results.\n" if (eof(EMPLOYEES)) +;

    But the bigger problem using the eof approach is that if the match is last line of the file - you'll get the match printed and the 'Sorry' message.



    grep
    One dead unjugged rabbit fish later

      Unless I'm missing something I think you meant

      For some odd reason, I thought <EMPLOYEES> contained the query results. Don't try to make sense of it for I was completely wrong.