比如说两个txt文件
test1是
id name height
1 张明 163
2 王明 177
test2是
id weight
1 55
3 70
想要的结果是
id name height weight
1 张明 163 55
2 王明 177
3 70
这样的
有没有大神指导一下
Perl中怎么样将txt文件中的两个数组 按相同字段匹配然后同行输出
答案:2 悬赏:50 手机版
解决时间 2021-12-23 02:01
- 提问者网友:动次大次蹦擦擦
- 2021-12-22 17:33
最佳答案
- 五星知识达人网友:長槍戰八方
- 2022-01-10 03:26
#!perl
use strict;
my %data;
open F, "test1" or die "Can't open test1";
while ( ) {
chomp;
my @parts = split /\s+/, $_;
my $id = shift @parts;
my $this = $data{$id} = {};
$this -> {name} = shift @data;
$this -> {height} = shift @data;
}
close F;
open F, "test2" or die "Can't open test2";
while ( ) {
chomp;
my @parts = split /\s+/, $_;
my $id = shift @parts;
$data{$id}{weight} = shift @parts;
}
close F;
print "id\tname\theight\tweight$/";
foreach ( sort {$a<=>$b} keys %data ) {
my $this = $data{$_};
print $_ . "\t" . $this->{name} .
$this->{height} . "\t" . $this->{$height} . $/;
}
use strict;
my %data;
open F, "test1" or die "Can't open test1";
while (
chomp;
my @parts = split /\s+/, $_;
my $id = shift @parts;
my $this = $data{$id} = {};
$this -> {name} = shift @data;
$this -> {height} = shift @data;
}
close F;
open F, "test2" or die "Can't open test2";
while (
chomp;
my @parts = split /\s+/, $_;
my $id = shift @parts;
$data{$id}{weight} = shift @parts;
}
close F;
print "id\tname\theight\tweight$/";
foreach ( sort {$a<=>$b} keys %data ) {
my $this = $data{$_};
print $_ . "\t" . $this->{name} .
$this->{height} . "\t" . $this->{$height} . $/;
}
全部回答
- 1楼网友:有你哪都是故乡
- 2022-01-10 03:56
可以私聊我~
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯