Volverine
Volverine

Reputation: 75

pattern not printing while trying to match same string on two files

I am trying to find pattern from two different files and then need to sum the last values of two filesand then print sum with first ,second and third field(sum) in xls...this is so far i wrote but it is printing value of first if loop not sure why not printing for second.if somone having better logic than below to do same operation ...

file1.txt 
L01B,"ABC",832048921.62
L01E,"DDD",70675364.68
L02A,"ZZZ",19747732853.37

file2.txt
L01B,"AAA",832048921.62
L01E,"DDD",70675364.68
L02A,"ZZZ",19747732853.37
#!/usr/bin/perl

use warnings;
use strict ;
  use Date::Simple qw(d8);

  my $firstdate = $ARGV[0];
  my $d1 = d8($firstdate);
 my $seconddate = $ARGV[1];
  my $d2 = d8($seconddate);
  my $f1=$d1-> format ('%d_%m_%Y');
  my $f2=$d2-> format ('%d_%m_%Y');
  print  " $f1 \t";
  print "$f2\n";
  open(my $fh1, '<', $f1) or die "Could not open file '$f1' $!";
  open(my $fh2, '<', $f2) or die "Could not open file '$f2' $!";

  while ( my $line1 = <$fh1>) {
           chomp $line1;
           if ($line1 =~/L01B/){
               my @array = split /[,]+/,$line1;
               {
               while (my $line2 = <$fh2>)
               {
               if ($line2 =~/L01B/){

               my @array1  = split /[,]+/,$line2;
               print " match found sec $array1[0]\n";
              print " match found sec $array1[1]\n";
              print " match found sec $array1[2]\n";
              print " match found first $array[0]\n";
              print " match found first $array[1]\n";
              print " match found first $array[2]\n";
              }

              }
              }
             }
             if ($line1 =~/L01B/){
               my @array2 = split /[,]+/,$line1;
               {
               while (my $line2 = <$fh2>)
               {
               if ($line2 =~/L01A/){

               my @array3  = split /[,]+/,$line2;
               print " match found sec $array2[0]\n";
              print " match found sec $array2[1]\n";
              print " match found sec $array2[2]\n";
              print " match found first $array3[0]\n";
              print " match found first $array3[1]\n";
              print " match found first $array3[2]\n";
              }

              }
              }

             }

             }
    close($fh1);
    close($fh2);        

Upvotes: 0

Views: 52

Answers (2)

praveenzx
praveenzx

Reputation: 28

For quick Fix, load the file contents to an array as follows

open(fh1, '<', $f1) or die "Could not open file '$f1' $!";
my @file1= <fh1>;
close(fh1);

open(fh2, '<', $f2) or die "Could not open file '$f2' $!";
my @file2= <fh2>;
close(fh2);

Now instead of while loop try and see using the foreach loop as shown below

foreach my $line1 (@file1)

@volverin : I have not tested the codes , just made some changes for you to refer .

praveenzx~

Upvotes: 0

choroba
choroba

Reputation: 241908

In the inner loop, you exhaust $fh2. In the next iteration of the outer loop, <$fh2> returns undef, as the filehandle has already read the whole file.

It's not clear what you're trying to achieve, but if the files are synchronised (i.e. nth line of file1 always corresponds to nth line in file2), you can just read one file from each handle; otherwise, the common practice is to hash one file and then process the second one line by line, checking the interesting values in the hash.

Upvotes: 1

Related Questions