【发布时间】:2018-05-11 00:00:47
【问题描述】:
我在 Perl 中有一个问题。 2 个文件。
文件A
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
文件B
DistA,1010101,a_0200,23
DistA,1010101,a_0300,21
DistA,1010101,b_0200,21
DistA,1010101,b_0300,19
我得到的结果是这样的。
文件C
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
但是我希望每个结果都恰好在这个序列中,并从 FileB 的第 6 列中减去 1,如果值等于 0,我不需要打印。这只是一个示例,因为我需要 FileA 的每一行的类似结果
我想根据以下字段的匹配将 FileA 递减到 0。
DistA,1010101,a_0200.
所以减量是这样的。
文件新建C
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
我的 perl 在下面。
#!/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;
}
}
【问题讨论】:
-
第二个 while 是我的 $max (@{ $hash{$dist, $sec, $cls} }){ my $tot = $qtd -1; print join(",", $dist, $sec, $cls, $add, $idx, $tot, $max )."\n"; $qtd=$tot; }
-
在 FileNewC 中没有最大值。你如何使用最大值?
标签: perl