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

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

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

        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 ?
      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
        basically it isnt moving to the next row