Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: grep in Perl

by Anonymous Monk
on Jun 05, 2015 at 09:18 UTC ( [id://1129178]=note: print w/replies, xml ) Need Help??


in reply to grep in Perl

I am new to perl scripting. I have 2 new input files, file1 and file2. I have to retrieve second field of each record from first file and check if it is existing in second file, file2. I have read file2 contents and stored it in an array. Please find my file2(npn.txt) contents below:

11111111 10001781 11111222
File1 contents:
abc,,,, abc,10001781,,, abc,10001782,,, abd,10001783,,,

Based on above files, what my record should contain:

abc,,,, abc,10001782,,, abd,10001783,,,
But it prints:
abc,10001781,,, abc,10001782,,, abd,10001783,,,

Can anyone please guide me what is the bug in this. Please find my code snippet below:

... open (info, "<npn.txt"); chomp (@contents = (<info>)); close info; $record = $_."~"; @flds = split(/\,/, $record); if (grep !/^$flds[1]/, @contents) { print $record; } ' >hi5res.txt

Replies are listed 'Best First'.
Re^2: grep in Perl
by aaron_baugher (Curate) on Jun 05, 2015 at 12:12 UTC

    Your grep returns all the elements of @contents that are NOT matched by your regex, so that will always be true. Removing the ! from your grep should fix that, assuming the rest of the code that we can't see is okay.

    This is not the best way to do this, though, because you're looping through the contents of one file for each line in the other file. The standard way to "match lines from one file to keys from another file" is to load the keys from one file (file2) into a hash as its keys, then go through the other file (file1) splitting out the matching part and seeing if it exists as a key in the hash. If it does exist, print it.

    Try coding it that way, and let us know if you need help.

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

Re^2: grep in Perl
by vinoth.ree (Monsignor) on Jun 05, 2015 at 10:45 UTC

    It should be working for you

    use strict; use warnings; my $second_file = "npn.txt"; open my $fh2, '<', $second_file or die "Can't write to '$second_file': + $!\n"; my @contents; chomp (@contents = (<$fh2>)); close $fh2; my $first_file= "hi5res.txt"; open my $fh1, '<', $first_file or die "Can't write to '$first_file': $ +!\n"; while( my $eachLine=<$fh1>) { chomp($eachLine); my @array = split(/,/,$eachLine); print $eachLine if (not defined $array[1]); if (grep {$_ eq $array[1]} @contents) { print "$eachLine\n" ; } }

    All is well. I learn by answering your questions...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (None)
    As of 2024-04-25 00:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found