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


In reply to Grep inside the while Loop by snape

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.