文件 1 的范围为 3-9、2-6 等
3 9
2 6
12 20
File2 有值:第 1 列表示范围,第 2 列有值。
1 4
2 4
3 5
4 4
5 4
6 1
7 1
8 1
9 4
我想计算file1中范围的值总和(file2,column2)。例如:如果范围是 3-9,那么值的总和将为 5+4+4+1+1+1+4 = 20
我尝试过的是:
open (FILE1,"file1.txt");
open (FILE2,"file2.txt");
@file1 = <FILE1>;
@file2 = <FILE2>;
foreach (@file1)
{
@split_file2 = split("\\s",$_); //splitting the file by space
foreach (@file2)
{
@split_file2 = split("\\s",$_); //splitting the file by space
if (@split_file1[0] == @split_file2[0]) //if column0 of file1 matches with column0 of file2
{
$x += @split_file2[1]; //sum the column1 of file2
if ( @split_file2[0] == @split_file1[0] ) //until column1 of file1 = column0 of file2.
{
last;
}
}
}}