Hello all, I am a newbie and this is my first post so thanks in advance for any help. I am having trouble with what should be an easy part of a larger script. The result of the code below is a file with more lines than elements in the array (@isos20) used in the grep. I have done this multiple ways and can't figure out why grep is matching more than one line for some elements (if that's really the problem). The lines of the file to read are formatted like this:

comp1234_c0_seq1,1,5,3,8,0,6,...

where the string before the first comma is an ID in which numbers vary but the letters are consistent throughout and that potentially exists in @isos20.

Here is the code I have now:

#open file to write filtered lines to open (OUT2, ">$pos_two") or die "cannot open $pos_two "; #open file to filter open (IN2, "<$posfile") or die "cannot open $posfile "; while (<IN2>) { chomp(my $line = $_); if ($line =~ m/(comp\d+_c\d+_seq\d+),.+/) { my $comp = $1; if (grep (/$comp/, @isos20)) { print OUT2 "$line\n"; } } } close IN2; close OUT2;

I can only guess that the grep is matching more than one line for some of the elements in @isos20 but I don't know why that would happen - unless the $comp variable is actually a regex and not a literal string. Thanks very much for any help. I know this must be a simple fix but I am at a loss.
Best, BD


In reply to grep of readline matching more lines than elements in array by bdorsey

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.