嗨,我有一个问题要解决,
我有2个文件。
文件A col1,col2,value_total_to_put
File A 201843,12345,30
文件B col1,col2,col3,value_inputted,missing_value,value_max
201843,12345,447,4,0,4
201843,12345,448,0,0,4
201843,12345,449,0,0,2
201843,12345,450,4,0,4
201843,12345,451,2,0,2
201843,12345,455,4,0,4
201843,12345,457,0,0,4
201843,12345,899,10,0,10
201843,12345,334,0,1,1
201843,12345,364,0,1,1
201843,12345,71,0,2,2
201843,12345,260,0,2,2
201843,12345,321,0,2,2
201843,12345,328,0,2,2
201843,12345,371,0,2,2
201843,12345,385,0,2,2
201843,12345,426,0,2,2
201843,12345,444,0,2,2
201843,12345,31,4,6,10
201843,12345,360,2,87,99
201843,12345,373,4,95,99
201843,12345,472,4,95,99
201843,12345,475,4,95,99
201843,12345,430,0,99,99
201843,12345,453,0,99,99
201843,12345,463,0,99,99
201843,12345,482,0,99,99
201843,12345,484,0,99,99
我的密钥是两个文件中的col1和col2,我在下面这样做,并且我的循环是错误的,因为当我从文件B到达EOF时,我的循环停止了。 我想要的是将文件A和B与$ col1和$ col2匹配,并且value_total_to_put> 0时,在每个循环中,当value_inputted小于value_max时,从文件B中提取value_inputed中的1。要从文件中提取,missing_value可能大于0。 对于结果,我将在value_inputted等于value_max时打印,换句话说,直到达到value_max或value_total_to_put为0的最后一个值。
while ( <FA> ){
chomp;
my($col1,$col2, $value_total_to_put) = split ",";
push @A, [$col1,$col2, $value_total_to_put];
}
my @B;
while ( <FB> ){
chomp;
my($col1,$col2,$col3, $value_inputted, $missing_value, $value_max) = split ",";
push @B, [$col1,$col2,$col3, $value_inputted, $missing_value, $value_max];
}
foreach my $line (@A){
my $idxl = @$line[0].",".@$line[1];
my $value_total_to_put = @$line[2];
while ($value_total_to_put > 0){
foreach my $row ( @B ){
if ( $idxr eq $idxl ){
my $idxr = @$row[0].",".@$row[1];
my $value_inputted = @$row[3];
my $value_max = @$row[5];
my $missing_value = @$row[4];
if ( ($value_inputted eq 0) and ($missing_value eq 0)){
#do_nothing
} elsif($value_inputted == $value_max){
#do_nothing
print join(",", $idxr, @$row[2],"Value_inputted: ".$value_inputted, "Missing_value: ".$missing_value, "Value_max:".$value_max, "Total: ".$value_total_to_put)."\n";
}else{
$value_inputted++;
$missing_value--;
$value_total_to_put--;
}
}
}
last if $value_total_to_put > 0;
}
}
第三个文件将是这样:
201843,12345,447,4,0,4
201843,12345,450,4,0,4
201843,12345,451,2,0,2
201843,12345,455,4,0,4
201843,12345,899,10,0,10
201843,12345,334,1,0,1
201843,12345,364,1,0,1
201843,12345,71,2,0,2
201843,12345,260,2,0,2
201843,12345,321,2,0,2
201843,12345,328,2,0,2
201843,12345,371,2,0,2
201843,12345,385,2,0,2
201843,12345,426,2,0,2
201843,12345,444,2,0,2
201843,12345,31,10,0,10
201843,12345,360,3,86,99
201843,12345,373,5,94,99
201843,12345,472,5,94,99
201843,12345,475,5,94,99
201843,12345,430,1,98,99
201843,12345,453,1,98,99
答案 0 :(得分:0)
正如@Dave Cross所解释的那样,您的代码很难阅读(错过了INSERT INTO tbl_name
(column1, column2, column3, ...)
VALUES
(Value1a, Value2a, Value3a, ...),
(Value1b, Value2b, Value2b, ...),
(Value1c, Value2c, Value2c, ...);
和use strict
),并且您对要实现的目标的解释还很不清楚... < / p>
但是引起我注意的一件事是,您使用此语句开始循环...
use warnings
...但是在同一块代码的最后,您要做的是:
while ($value_total_to_put > 0){
这将有效地导致Perl在第一次迭代后退出循环,无论 last if $value_total_to_put > 0;
}
变量的值如何。这可能不是您想要的。因此,据我了解,您应该通过删除该$value_total_to_put
语句来开始调查。