循环遍历2个文件perl

时间:2018-05-09 18:07:16

标签: perl

我在Perl中遇到了一个问题。 2个文件。

FILEA

construct_form

FILEB

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

我得到的就是这个。

FileC

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

但是我希望这个结果完全在这个序列中,并从FileB的第6列减去1,如果值等于0,我不需要打印。这只是一个例子,因为我需要为FileA的每一行提供类似的结果

我想根据以下字段的匹配将FileA减少到0。

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

所以减量就是这样。

FileNewC

DistA,1010101,a_0200.

我的perl在下面。

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

1 个答案:

答案 0 :(得分:1)

我认为你想在这种情况下使用数组......所以代码看起来如下。

#!/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;
    }
}

查看额外的详细信息调用,第三个参数设置为&#39; d&#39;

<filename> FileA FileB d

最终输出已配置为文件前缀,而行#用于说明正在修改的内容。

loop-1的详细示例是 (注意修改后的行以*开头)

如果这不符合您的预期,那么我们必须对从FileA创建的2d阵列进行预排序。如果是这种情况,则必须指定所需的排序顺序。

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