ebk
ebk

Reputation: 49

Loop through 2 files perl

I have an issue in Perl. 2 files.

FileA

DistA,1010101,a_0200,Address,11,7
DistA,1010101,a_0200,Address,12,7
DistA,1010101,a_0200,Address,09,3
DistA,1010101,a_0200,Address,10,3
DistA,1010101,a_0200,Address,13,2
DistA,1010101,a_0300,Address,11,6
DistA,1010101,a_0300,Address,12,6
DistA,1010101,a_0300,Address,09,3
DistA,1010101,a_0300,Address,10,3
DistA,1010101,a_0300,Address,13,2
DistA,1010101,b_0200,Address,11,6
DistA,1010101,b_0200,Address,12,6
DistA,1010101,b_0200,Address,09,3
DistA,1010101,b_0200,Address,10,3
DistA,1010101,b_0200,Address,13,2
DistA,1010101,b_0300,Address,11,6
DistA,1010101,b_0300,Address,12,6
DistA,1010101,b_0300,Address,09,3
DistA,1010101,b_0300,Address,10,3
DistA,1010101,b_0300,Address,13,2

FileB

DistA,1010101,a_0200,23 
DistA,1010101,a_0300,21
DistA,1010101,b_0200,21 
DistA,1010101,b_0300,19

and the resulting I have is this.

FileC

DistA,1010101,a_0200,Address,11,6
DistA,1010101,a_0200,Address,12,6
DistA,1010101,a_0200,Address,09,2
DistA,1010101,a_0200,Address,10,2
DistA,1010101,a_0200,Address,13,1
DistA,1010101,a_0300,Address,11,5
DistA,1010101,a_0300,Address,12,5
DistA,1010101,a_0300,Address,09,2
DistA,1010101,a_0300,Address,10,2
DistA,1010101,a_0300,Address,13,1
DistA,1010101,b_0200,Address,11,5
DistA,1010101,b_0200,Address,12,5
DistA,1010101,b_0200,Address,09,2
DistA,1010101,b_0200,Address,10,2
DistA,1010101,b_0200,Address,13,1
DistA,1010101,b_0300,Address,11,5
DistA,1010101,b_0300,Address,12,5
DistA,1010101,b_0300,Address,09,2
DistA,1010101,b_0300,Address,10,2
DistA,1010101,b_0300,Address,13,1

But I want this result for each exactly at this sequence and subtracting 1 from 6th column of the FileB and if the value is equal to 0 I do not need to print. This is only an example because I need the similar result for each line of the FileA

I want to decrement FileA until 0 based on the match for the following fields.

DistA,1010101,a_0200.

So the decrement is that way.

FileNewC

Loop-1
DistA,1010101,a_0200,Address,11,7
DistA,1010101,a_0200,Address,12,7
DistA,1010101,a_0200,Address,09,3
DistA,1010101,a_0200,Address,10,3
DistA,1010101,a_0200,Address,13,2
Loop-2
DistA,1010101,a_0200,Address,11,6
DistA,1010101,a_0200,Address,12,6
DistA,1010101,a_0200,Address,09,2
DistA,1010101,a_0200,Address,10,2
DistA,1010101,a_0200,Address,13,1
Loop-3
DistA,1010101,a_0200,Address,11,5
DistA,1010101,a_0200,Address,12,5
DistA,1010101,a_0200,Address,09,1
DistA,1010101,a_0200,Address,10,1
Loop-4
DistA,1010101,a_0200,Address,11,4
DistA,1010101,a_0200,Address,12,4
Loop-5
DistA,1010101,a_0200,Address,11,3
DistA,1010101,a_0200,Address,12,3
Loop-6
DistA,1010101,a_0200,Address,11,2
DistA,1010101,a_0200,Address,12,2
Loop-7
DistA,1010101,a_0200,Address,11,1
DistA,1010101,a_0200,Address,12,1

My perl is below.

#!/usr/bin/perl

use strict;
use warnings;

$|=1;

my $filea = $ARGV[0];
my $fileb = $ARGV[1];


open ( FA, '<', $filea) || die ( "File $filea Not Found!" );
open ( FB, '<', $fileb) || die ( "File $fileb Not Found!" );
#open ( FC, ">", $filec) || die ( "File $filec Not Found!" );

my %hash;
while ( <FB> ) {
    chomp;
    my($dist, $sec, $cls, $max) = split ",";
    push @{ $hash{$dist, $sec, $cls} }, $max;
}

while ( <FA> ) {
    chomp;
    my($dist, $sec, $cls, $add, $idx, $qtd) = split ",";

    for my $max ( @{ $hash{$dist, $sec, $cls } }){
        my $tot = $qtd -1;
        print join(",", $dist, $sec, $cls, $add, $idx, $tot)."\n";
         $qtd=$tot;

    }
}

Upvotes: 0

Views: 85

Answers (1)

webmite
webmite

Reputation: 575

I think you want to use arrays in the case...so the code would look as follows.

#!/usr/bin/perl

use strict;
use warnings;

$|=1;

my $filea = $ARGV[0];
my $fileb = $ARGV[1];
my $action = $ARGV[2] || "n" ; 

open ( FA, '<', $filea) || die ( "File $filea Not Found!" );
open ( FB, '<', $fileb) || die ( "File $fileb Not Found!" );
#open ( FC, ">", $filec) || die ( "File $filec Not Found!" );

my @B;
while ( <FB> ) {
    chomp;
    my($dist, $sec, $cls, $max) = split ",";
    push @B, [$dist, $sec, $cls, $max];
}

my @A;
while ( <FA> ) {
    chomp;
    my($dist, $sec, $cls, $add, $idx, $qtd) = split ",";
    push @A, [$dist, $sec, $cls, $add, $idx, $qtd];
}

$i = 1;
my $j = 0;
my $k = 0;
while ( 1 ) {
    # -- keep looping til nothing is modified --
    my $modified=0;
    $j = 0;
    foreach my $row ( @A ) {
        # -- loop through FileA, $j is rowcount --
        $j++;
        $k=0;
        foreach my $line ( @B ) {
            # -- loop through FileB, $k is linecount --
            $k++;
            my $idx1= @$line[0].@$line[1].@$line[2];
            my $idx2= @$row[0].@$row[1].@$row[2];
            if ($idx1 eq $idx2) {
                # -- has to match on the index fields --
                my $max = @$line[3];
                my $tot = @$row[5] -1;
                if ( $tot > 0 ) {
                    # -- only print if modified and result is >0 --
                    # -- only print loop header if 1rst time through inside loop --
                    if (! $modified) { print "Loop-".$i++."\n"; }
                    if ($action eq "d") {
                        print "FileB[".$k."]: ".join(",", @$line[0],@$line[1],@$line[2],@$line[3] )."\n";
                        print "FileA[".$j."]: ".join(",", @$row[0],@$row[1],@$row[2],@$row[3],@$row[4],@$row[5]  )."\n";
                    }
                    print "*FileA[".$j."]: ".join(",", @$row[0],@$row[1],@$row[2],@$row[3],@$row[4],$tot )."\n";
                    @$row[5]=$tot;
                    $modified = 1;
                }
            }
        }
    }
    if ((! $modified ) || ($i > 50)) {
        # -- only exit if pass through entire loop with no modifications --
        # -- or we have looped too many times ... sanity check --
        last;
    }
}

to see extra detail call with 3rd argument set to 'd'

<filename> FileA FileB d

final output has been configured to prefix the file and Line# to clarify what is being modified.

Example of detail for loop-1 is (note the modified lines begin with a *)

If this is NOT in the order you expect then we have to pre-sort the 2d-array created from FileA. If that is the case you have to specify the sort order required.

Loop-1
FileB[1]: DistA,1010101,a_0200,23 
FileA[1]: DistA,1010101,a_0200,Address,11,7
*FileA[1]: DistA,1010101,a_0200,Address,11,6
FileB[1]: DistA,1010101,a_0200,23 
FileA[2]: DistA,1010101,a_0200,Address,12,7
*FileA[2]: DistA,1010101,a_0200,Address,12,6
FileB[1]: DistA,1010101,a_0200,23 
FileA[3]: DistA,1010101,a_0200,Address,09,3
*FileA[3]: DistA,1010101,a_0200,Address,09,2
FileB[1]: DistA,1010101,a_0200,23 
FileA[4]: DistA,1010101,a_0200,Address,10,3
*FileA[4]: DistA,1010101,a_0200,Address,10,2
FileB[1]: DistA,1010101,a_0200,23 
FileA[5]: DistA,1010101,a_0200,Address,13,2
*FileA[5]: DistA,1010101,a_0200,Address,13,1
FileB[2]: DistA,1010101,a_0300,21
FileA[6]: DistA,1010101,a_0300,Address,11,6
*FileA[6]: DistA,1010101,a_0300,Address,11,5
FileB[2]: DistA,1010101,a_0300,21
FileA[7]: DistA,1010101,a_0300,Address,12,6
*FileA[7]: DistA,1010101,a_0300,Address,12,5
FileB[2]: DistA,1010101,a_0300,21
FileA[8]: DistA,1010101,a_0300,Address,09,3
*FileA[8]: DistA,1010101,a_0300,Address,09,2
FileB[2]: DistA,1010101,a_0300,21
FileA[9]: DistA,1010101,a_0300,Address,10,3
*FileA[9]: DistA,1010101,a_0300,Address,10,2
FileB[2]: DistA,1010101,a_0300,21
FileA[10]: DistA,1010101,a_0300,Address,13,2
*FileA[10]: DistA,1010101,a_0300,Address,13,1
FileB[3]: DistA,1010101,b_0200,21 
FileA[11]: DistA,1010101,b_0200,Address,11,6
*FileA[11]: DistA,1010101,b_0200,Address,11,5
FileB[3]: DistA,1010101,b_0200,21 
FileA[12]: DistA,1010101,b_0200,Address,12,6
*FileA[12]: DistA,1010101,b_0200,Address,12,5
FileB[3]: DistA,1010101,b_0200,21 
FileA[13]: DistA,1010101,b_0200,Address,09,3
*FileA[13]: DistA,1010101,b_0200,Address,09,2
FileB[3]: DistA,1010101,b_0200,21 
FileA[14]: DistA,1010101,b_0200,Address,10,3
*FileA[14]: DistA,1010101,b_0200,Address,10,2
FileB[3]: DistA,1010101,b_0200,21 
FileA[15]: DistA,1010101,b_0200,Address,13,2
*FileA[15]: DistA,1010101,b_0200,Address,13,1
FileB[4]: DistA,1010101,b_0300,19
FileA[16]: DistA,1010101,b_0300,Address,11,6
*FileA[16]: DistA,1010101,b_0300,Address,11,5
FileB[4]: DistA,1010101,b_0300,19
FileA[17]: DistA,1010101,b_0300,Address,12,6
*FileA[17]: DistA,1010101,b_0300,Address,12,5
FileB[4]: DistA,1010101,b_0300,19
FileA[18]: DistA,1010101,b_0300,Address,09,3
*FileA[18]: DistA,1010101,b_0300,Address,09,2
FileB[4]: DistA,1010101,b_0300,19
FileA[19]: DistA,1010101,b_0300,Address,10,3
*FileA[19]: DistA,1010101,b_0300,Address,10,2
FileB[4]: DistA,1010101,b_0300,19
FileA[20]: DistA,1010101,b_0300,Address,13,2
*FileA[20]: DistA,1010101,b_0300,Address,13,1
Loop-2

Upvotes: 1

Related Questions