|Perl Monk, Perl Meditation|
Re: How to grep exact stringby ColonelPanic (Friar)
|on Nov 15, 2012 at 08:46 UTC||Need Help??|
This is a perfect case for the smart match operator. You can do it like this:
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:
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:
When's the last time you used duct tape on a duct? --Larry Wall