coldy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I would like to emulate
fgrep -w -f list.txt largefile.txt
but the results must be in the same order as they appear in list.txt. I have tried
my @list = <$list>; my @large = <$large>; foreach my $test (@list){ print grep (/^$test/,@large); }
but this does not work... the file $list is a single field and the file $large has several fields but is matched to $list by the first field. Any advice would be appreciated. Thanks..

Replies are listed 'Best First'.
Re: emulate "fgrep -f file" with results ordered by "file"
by toolic (Bishop) on Mar 15, 2011 at 02:10 UTC
    but this does not work...
    That is not descriptive enough, and you haven't shown enough of your code, but here are a couple suggestions:
      thanks for the suggestion - chmop worked. There is one other problem - it is fairly slow. There are about 1M itmes to grep from @list and @large has a few more lines than that (about 1.2M )..any ideas on how to do this more efficiently?

        You could create an index (=hash lookup table) by the first field of 'large'.  Then loop over @list and check if the key exists in the lookup table.

        Something like

        my %index; while (<DATA>) { # your $large my ($f1) = split; $index{$f1} .= $_; } my @list = qw(foo r2c1 bar r3c1); for (@list) { print $index{$_} if exists $index{$_}; } __DATA__ r1c1 r1c2 r1c3 r2c1 r2c2 r2c3 r3c1 r3c2 r3c3