作者: hubenxia 时间: 2014-07-22 15:17
#!/usr/bin/perl -w
my %hash;
my $value;
open IN2,"<2.txt";
open OUT1,">out1.txt";
open IN1,"<1.txt";
my @value;
while (<IN2>) {
chomp;
my($key,@value)=split/ /,$_,2;
$value=join "t",@value;
$hash{$key}=$value;
}
while (<IN1>) {
chomp;
if (exists $hash{$_}) {
$value=$hash{$_};
}
}
foreach my $key (keys %hash) {
print OUT1 "$key\t$hash{$key}\n";
}
close IN2;
close IN1;
close OUT1;作者: hubenxia 时间: 2014-07-22 15:32
#!/usr/bin/perl -w
my %hash;
my $value;
open IN2,"<2.txt";
open OUT1,">out1.txt";
open IN1,"<1.txt";
my @value;
while (<IN2>) {
chomp;
my($key,@value)=split/ /,$_,2;
$value=join "t",@value;
$hash{$key}=$value;
}
foreach my $key(<IN1>) {
chomp $key;
print "$key\n";
print OUT1 "$key\t$hash{$key}\n";
}
close IN2;
close IN1;
close OUT1; 这个比较简单。作者: reyleon 时间: 2014-07-23 10:33
作者: Monday0 时间: 2014-11-12 16:30
我的确是按照代码运行的,可为什么这次运行结果什么也没有呢
#!/usr/bin/perl -w
use strict;
my $file_1=shift;
my $file_2=shift;
open(IN2,"$file_2")||die"$!\n";
my %hash;
while (<IN2>){
chomp;
my @a=split /\t/,2;
$hash{$a[0]} = $a[1];
}close IN2;
open(IN1,"$file_1")||die"$!\n";
while (<IN1>)
{
chomp;
print "$_\t$hash{$_}\n" if exists $hash{$_};
}
close IN1; 回复 10# 清泉一边
作者: ntwarren 时间: 2014-11-12 21:41
#!/usr/bin/perl
open IN2,"<","file2";
while(<IN2>){
chomp;
my @a=split /[ \t]+/,$_;
$hash{$a[0]}=[$a[1],$a[2]];
}
open IN1,"<","file1";
my @b=<IN1>;
chomp @b;
for my $c(@b){
print "$c\t @{$hash{$c}}";
}