Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How to grep exact string

by ColonelPanic (Friar)
on Nov 15, 2012 at 08:46 UTC ( #1003961=note: print w/ replies, xml ) Need Help??


in reply to How to grep exact string

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


Comment on Re: How to grep exact string
Select or Download Code
Re^2: How to grep exact string
by tobyink (Abbot) on Nov 15, 2012 at 09:06 UTC

    The behaviour of smart match on arrays may well change in Perl 5.18 or 5.20.

    This works quite nicely for me...

    use strict; use warnings; use File::Slurp qw( slurp ); use Syntax::Keyword::Junction qw( any ); my @first = map { lc($_) } slurp('file1.txt'); my @second = map { lc($_) } slurp('file2.txt'); chomp(@first, @second); print "Machines extra in second list...\n"; for my $machine (@second) { if ($machine eq any(@first)) { print "$machine is already there\n"; } else { print "$machine is new\n"; } }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      I think (hope!) that smart match is mature enough at this point that you can rely on its behavior not changing for such a basic case.

      Nevertheless, another way of solving the problem is always useful.



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

        I imagine that Perl will continue to support the old behaviour by default and an explicit use 5.xxx statement will enable the new behaviour via feature. (Much like how use 5.010 enables state, say, etc.)

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Wasn't familiar with Syntax::Keyword::Junction, so I appreciate seeing it used here. Also, File::Slurp's a good module to use...

      Since the OP's comparing lists, List::Compare can help find unique entries in file2.txt:

      use Modern::Perl; use File::Slurp qw/read_file/; use List::Compare; my %hash; my @file1 = map { chomp; lc } read_file 'file1.txt'; my @file2 = map { chomp; lc } read_file 'file2.txt'; my $lc = List::Compare->new( \@file1, \@file2 ); my @file2Only = $lc->get_Ronly; say for @file2Only;

      Output:

      nt64osbld2 nt64osbld3 nt64oscitrix1 ntosbld4 ntosbld5 ntosbld6 vm-osw2k8x64-1 vm-osw2k8x64-2

      Not in the OP's output formatting, but that wouldn't take much more...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003961]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-09-17 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (57 votes), past polls