http://www.perlmonks.org?node_id=866426

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

It is more of a conceptual question rather than a logic question. I have 2 while loops in my program below. The first while loop is used to store the set of words in an array after opening the first file. The second while loop is used to find if there is a match between the the words in the array with the words in the the 3rd column of second file.

I used grep inside the while loop for checking the "matched text". My code is as follows:

my @list_match; my @matchArray; open IN, "Data.txt" or die $!; # The file here has one column while(<IN>){ chomp($_); push @list_match, $_; } close(IN); open INPUT, "Data_Large.txt" or die $!; # It is a tab delimited file having 7 columns. I am looking # to match + the 3rd column words with the above # @list_match array while(<INPUT>){ chomp($_); my @arrList = split('\t', $_); print "Found $arrList[2]", if (grep {$_ eq $arrList[2]} @list_match) +## assuming it is a character } close(INPUT);

Now if I use the above code then it doesn't find the "match word". I think it is assuming "$_" as the entire current line of the file. My question is why doesn't it assume as the current index of the array @list_match. If you use the same convention of grep in comparing the 2 arrays then it will work. For example:

for (my $i = 0; $i <= 10; $i++){ print "Found $arrList[$i]" if (grep {$_ eq $arrList[$i]} @list_match) + ## assuming that @arrList is an array of column 3 elements }

I know there is another way of using grep like

grep {/$arrList[2]/} @list_match ## if I am searching for $arrList[2]

but I would like to know why the prior grep syntax in the while loop doesn't work. Thanks a lot.

UPDATE : I am sorry, my code is correct and there is no collision of $_. The above code works. I did a mistake while I was coding before. Sorry for the inconvenience. Thanks