Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^8: Filtering Output from two files

by roboticus (Chancellor)
on Feb 06, 2018 at 08:47 UTC ( [id://1208547]=note: print w/replies, xml ) Need Help??


in reply to Re^7: Filtering Output from two files
in thread Filtering Output from two files

vighneshmufc:

You have an error in this line:

while (my ($row) = <$fh2>) {

Since $row is in parenthesis, you're doing an assignment in list context. So all the lines in $fh2 are read, and the first one is passed into $row, and the rest are discarded.

Change the line to:

while (my $row = <$fh2>) {

and then it should work better for you.

Here's a little demonstration:

$ cat t.pl use strict; use warnings; # Scalar context my $row = <DATA>; print $row; # Array context my ($a) = <DATA>; print $a; # Now there's nothing left! my $c = <DATA>; print $c; __DATA__ Now is the time for all good men to come to the aid of their party $ perl t.pl Now is the time for all good men Use of uninitialized value $c in print at t.pl line 14, <DATA> line 5.

Notice that we read the first line in scalar context, and then we print the first line successfully.

Next, we read a line in array context, and print the second line successfully.

Finally, we try to read the next line, but there's no data left! All of it was read earlier!

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^9: Filtering Output from two files
by Anonymous Monk on Feb 06, 2018 at 09:12 UTC
    thank you i am able to do it
    now i wanna print the matching lines
    print $row if exists $hash{$fields[0]}; something wrong with this syntax ?
      i have excluded the part where i load the files to a hash i just wanna print the same values from both table

        vighneshmufc:

        The syntax you showed on your previous message looks fine to just print the row that matches.

        In this message, though, you're saying you want to print the same values from both tables. To do that, you can use:

        print $fields[0], " ", $row if exists $hash{$fields[0]};

        It's redundant, though, since the first column in $row (i.e. $fields[0]) is also the same value as you had in the first table.

        Suppose, however, your first file contained the key *and* some values, like the second file does. In that case, you need to change your first loop that loads up the hash. You'd split it on the delimiter just like you do for the second file, and then rather than just using $hash{$line} = 1, you'd store the value(s) you want, like:

        my %hash=(); open (my $fh,'<',$file2) or die $!; while(my $line=<$fh>) { chomp $line; # Split the line into fields (first field is the key) my @fields = split /\|/, $line; # store the entire line in the hash under the key $hash{$fields[0]} = $line; } close $fh; print "KEYS AND RECORDS ARE:\n"; print Dumper(\%hash);

        Once you have all your data in your hash, then you can print both rows when the keys match:

        print $hash{$fields[0]}, " ", $row if exists $hash{$fields[0]};

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found