求助Perl语言解决问题
file11256438924.....file212349......110.10.20.60.2...20.110.30.20.3...30.20.310.20.1....
file1
1 2
5 6
4 3
8 9
2 4
.. ...
file2
1 2 3 4 9 .. ....
1 1 0.1 0.2 0.6 0.2 ...
2 0.1 1 0.3 0.2 0.3 ...
3 0.2 0.3 1 0.2 0.1 ....
4 0.6 0.2 0.2 1 0.3 ...
9 0.2 0.3 0.1 0.3 1 ...
.. ... .... ..... ..... ....
要求是,若file1的每一行中的两个数都能在file2中找到,则以下面格式输出到file3文件中
file3
1 2 0.1
4 3 0.2
.. ... ...
file3
1 2 0.1
4 3 0.2
2 4 0.2
.. ... ... 展开
1 2
5 6
4 3
8 9
2 4
.. ...
file2
1 2 3 4 9 .. ....
1 1 0.1 0.2 0.6 0.2 ...
2 0.1 1 0.3 0.2 0.3 ...
3 0.2 0.3 1 0.2 0.1 ....
4 0.6 0.2 0.2 1 0.3 ...
9 0.2 0.3 0.1 0.3 1 ...
.. ... .... ..... ..... ....
要求是,若file1的每一行中的两个数都能在file2中找到,则以下面格式输出到file3文件中
file3
1 2 0.1
4 3 0.2
.. ... ...
file3
1 2 0.1
4 3 0.2
2 4 0.2
.. ... ... 展开
展开全部
open (F,"file2");
@area = <FF>;
close FF;
@col_title = split " ",$area[0];
my %hash_row_title;
for($i=0;$i<=$#col_title;$i++)
{
local $t = $col_title[$i];
$hash_row_title{$t} = $i;
}
#print "---\n";
my %hash_col_title;
my @AoA;
$cur_row = 0;
for($i=1;$i<=$#area;$i++)
{
local @cols = split " ",$area[$i];
$hash_col_title{$cols[0]} = $i-1;
for($j = 1;$j<=$#cols;$j++)
{
$AoA[$cur_row][$j - 1] =$cols[$j];
}
$cur_row++;
}
#print $AoA[3][4];
#print $hash_row_title{4};
open (FF,"file1");
@coordinate = <F>;
close F;
open (FFF,">file3");
foreach $row (@coordinate)
{
local @cols = split " ", $row;
$xval = $hash_row_title{$cols[0]};
$yval = $hash_row_title{$cols[1]};
#print FFF $xval . " " . $yval . " " .$AoA[$xval][$yval] . "\n";
if(($xval ne "" ) and ($yval ne ""))
{
print FFF $xval . " " . $yval . " " .$AoA[$xval][$yval] . "\n";
}
}
close FFF;
@area = <FF>;
close FF;
@col_title = split " ",$area[0];
my %hash_row_title;
for($i=0;$i<=$#col_title;$i++)
{
local $t = $col_title[$i];
$hash_row_title{$t} = $i;
}
#print "---\n";
my %hash_col_title;
my @AoA;
$cur_row = 0;
for($i=1;$i<=$#area;$i++)
{
local @cols = split " ",$area[$i];
$hash_col_title{$cols[0]} = $i-1;
for($j = 1;$j<=$#cols;$j++)
{
$AoA[$cur_row][$j - 1] =$cols[$j];
}
$cur_row++;
}
#print $AoA[3][4];
#print $hash_row_title{4};
open (FF,"file1");
@coordinate = <F>;
close F;
open (FFF,">file3");
foreach $row (@coordinate)
{
local @cols = split " ", $row;
$xval = $hash_row_title{$cols[0]};
$yval = $hash_row_title{$cols[1]};
#print FFF $xval . " " . $yval . " " .$AoA[$xval][$yval] . "\n";
if(($xval ne "" ) and ($yval ne ""))
{
print FFF $xval . " " . $yval . " " .$AoA[$xval][$yval] . "\n";
}
}
close FFF;
更多追问追答
追问
非常感谢,但file3文件最后结果是这样的
file3
0 1 0.1
1 3 0.2
我想要的是下面这样的结果
file4
1 2 0.1
4 3 0.2
2 4 0.2
.. ... ...
追答
把 print FFF $xval . " " . $yval . " " .$AoA[$xval][$yval] . "\n";
更改为:
print FFF $cols[0] . " " . $cols[1] . " " .$AoA[$xval][$yval] . "\n";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询