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

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.

Replies are listed 'Best First'.
Re^6: Filtering Output from two files
by Anonymous Monk on Feb 06, 2018 at 06:04 UTC
    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

        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.

        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| |
        Sorry this is the file