in reply to searching a file

my @list = qw(value1 value2); unless (open (FILE, $filename)){ die("Error in opening $filename: $!\n"); } # if else { while (<FILE>){ chomp($_); # split up to maximal "two columns" by whitespaces my ($str, $val) = split(/\s+/, $_, 2); # search @list for $val if ( grep { $val eq $_ } @list ){ print "$val found\n"; } } # while close (FILE); } # else
If you don't need $str in your code, write something like this:
... while (<FILE>){ chomp($_); # split up to maximal "two columns" by whitespaces my ($val) = ( split(/\s+/, $_, 2) )[1]; # search @list for $val if ( grep { $val eq $_ } @list ){ print "$val found\n"; } } # while } # else

I haven't tested these codes.

Best regards,
perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Replies are listed 'Best First'.
Re: Re: searching a file
by tachyon (Chancellor) on Mar 19, 2002 at 18:43 UTC

    If you find yourself doing a grep inside a loop you essentially have an inefficient solution as you have one loop inside another (n*n) Paying the one off price to genreate a hash lookup table (or better still storing your data in this form) makes for far greater efficiency. An alternative in some instances is to build and compile a matching regex (outside the loop!) and use this in place of the grep/inner loop.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print