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

use strict; <br> use warnings; <br> use Data::Dumper; <br> my $file1 = 'file1';<br> my $file2 = 'file2'; <br> #reading file1 into a hash<br> my %hash;<br> open (my $fh,'<',$file2) or die $!;<br> while(my $line=<$fh>)<br> {<br> chomp $line;<br> $hash{$line}=1;<br> print Dumper %hash;<br> }<br> close $fh;<br> #reading file2 line by line <br> open (my ($fh2),'<',$file1) or die $!; <br> while (my ($row) = <$fh2>) {<br> chomp $row;<br> # next if $row =~ /^\s*$/;<br> my (@fields) = split(/\|/, $row);<br> print $row if exists $hash{$fields[0]};<br> }<br> close $fh2;<br>

I aint getting any O/P plus when i print Dumper %hash i get loads of $VAR
What am i doing wrong?

Replies are listed 'Best First'.
Re^5: Filtering Output from two files
by roboticus (Chancellor) on Feb 06, 2018 at 05:09 UTC

    Anonymous Monk:

    What am i doing wrong?
    1. When you're calling Dumper, you're giving it a hash as an argument. The hash expands to a list of key/value pairs, and each of those is being printed as a separate value. You want to pass it a reference to the hash, like:
      print Dumper(\%hash);
    2. You should put your print statement after the end of your loop. As it is now, you're printing the hash after adding each line to it, so you're getting far more output than you really want. (Unless, of course, you're really wanting to see the hash after each line is added.)
    3. If you're on a windows box, then chomp may not be removing all the whitespace at the end of the line. I generally avoid chomp and use something like:
      $line =~ s/\s+$//; # trim whitespace

    If you're still not getting output, you may want to add:

    print "<$fields[0]>\n";
    in the loop where you're searching for, so you can see what key is being looked for, and compare that against the keys displayed by Dumper previously.

    ...roboticus

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

      damn just realised i messed up the print statement inside the loop as u said i had to put it out of the loop. sorry i am very new to programming hence the mistakes
        OJL38 4|700000|10000000|25000000|6000000|Y|Y|Y|Debt Instruments: Bonds| |94507000|1350100000|3375250000|810060000|DBFTFB| | UJL38 4|700000|10000000|25000000|6000000|Y|Y|Y|Debt Instruments: Bonds| |94507000|1350100000|3375250000|810060000|DBFTFB| | MJL38 4|700000|10000000|25000000|6000000|Y|Y|Y|Debt Instruments: Bonds| |94507000|1350100000|3375250000|810060000|DBFTFB| | OJL384|700000|10000000|25000000|6000000|Y|Y|Y|Debt Instruments: Bonds| |94507000|1350100000|3375250000|810060000|DBFTFB| | This is one file i have when i run this script
        #!/nairvig/bin/perl use strict; use warnings; use Data::Dumper; my $file1 = 'file4'; my $file2 = 'file3'; open (my ($fh2),'<',$file1) or die $!; while (my ($row) = <$fh2>) { chomp $row; my (@fields) = split(/\|/, $row); next if $row =~ /^\s*$/; print "<$fields[0]>\n "; #print $row if exists $hash{$fields[0]}; } close $fh2;
        the O/p i get is this <OJL38 4> i wanna see all the 4 values from first column