EBK has asked for the wisdom of the Perl Monks concerning the following question:
l100101,aaaaaaa,a_0100,loc,10,1
l100101,aaaaaaa,a_0100,loc,11,1
l100101,aaaaaaa,a_0100,loc,12,6
File "B"l103709,bbbbbbb,c_0200,929
l100109,bbbbbbb,b_0100,442
l100107,bbbbbbb,c_0300,389
#!/usr/bin/perl use strict; use warnings; $|=1; my $filea = $ARGV[0]; my $fileb = $ARGV[1]; my $FileC = "result.csv"; 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($look, $sec, $cls, $max) = split ","; push @B, [$look, $sec, $cls, $max]; } my @A; while ( <FA> ) { chomp; my($look, $sec, $cls, $att, $idx, $qtd) = split ","; push @A, [$look, $sec, $cls, $att, $idx, $qtd]; } my $i = 1; my $j = 0; my $k = 0; my $count=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; # -- loop through FileB, $k is linecount -- foreach my $line ( @B ) { $k++; my $idx1= @$line[0].@$line[1].@$line[2]; my $idx2= @$row[0].@$row[1].@$row[2]; # -- has to match on the index fields -- if ($idx1 eq $idx2) { my $max = @$line[3]; my $tot = @$row[5] -1; last if $count == $max; if ( $tot >= 0 ) { #print "FileA[".$j."]: ".join(",", @$row[0],@$ +row[1],@$row[3],@$row[4],@$row[5] )."\n"; print FC join(",", @$row[0],@$row[1],@$row[3], +@$row[4],$max )."\n"; $count++; @$row[5]=$tot; $modified = 1; } } } } if ((! $modified ) || ($i > 10)) { last; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: how to avoid full scan in file.
by haukex (Archbishop) on May 25, 2019 at 09:08 UTC | |
Re: how to avoid full scan in file.
by LanX (Saint) on May 24, 2019 at 23:24 UTC | |
by EBK (Sexton) on May 25, 2019 at 00:01 UTC | |
Re: how to avoid full scan in file.
by Cristoforo (Curate) on May 25, 2019 at 01:13 UTC | |
by EBK (Sexton) on May 25, 2019 at 03:54 UTC | |
by poj (Abbot) on May 25, 2019 at 14:38 UTC | |
by EBK (Sexton) on May 25, 2019 at 16:43 UTC | |
by poj (Abbot) on May 25, 2019 at 17:15 UTC | |
| |
by EBK (Sexton) on May 25, 2019 at 18:00 UTC |