Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
This is a perfect case for the smart match operator. You can do it like this:
foreach my $unique (@second_list_new) { if ($unique ~~ @first_list_new)) { print "already there $unique\n\n"; } else { print "newly in this $unique\n\n"; } }
This checks if any of @first_list_new is equal to $unique. Unlike grep, it will stop as soon as it finds a match.

If you do stick with the regex method, it would be a good idea to use \Q...\E around your variable:
if (grep (/^\Q$unique\E$/,@first_list_new))
This ensures that it will match the literal content of $unqiue. Otherwise, some characters within $unique could be interpreted as special regex characters. This wouldn't be a problem with the sample data you have shown, but if you were searching for a value like "foo.bar", for example, it wouldn't do what you want without \Q...\E, because the . would match any character rather than a literal dot.

Update: Of course, this doesn't answer why your original match failed. Could it be a whitespace problem? The second file that you posted above seems to have spaces at the end of the lines. If this is the problem, you could either remove whitespace when you read the files in, or ignore it in your comparison.

Here is an example of how you could remove trailing whitespace from one of the files, as well as do all of your other processing in one go. Note that you no longer need to chomp, because the m switch on the regex causes the newline to also be matched and removed:
my @first_list_new = map { s/\s*$//m; lc $_ } <FIRST_LIST>;


When's the last time you used duct tape on a duct? --Larry Wall

In reply to Re: How to grep exact string by ColonelPanic
in thread How to grep exact string by Divakar

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2014-12-21 19:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (107 votes), past polls