Chinaunix

标题: 哈希求助 [打印本页]

作者: 静女,其姝    时间: 2014-07-22 00:16
标题: 哈希求助
file1:
C
X
A
F

file2:
A      3      2
C      6      5
F      1      2
X      2      4

输出:
C      6      5
X      2      4
A      3      2
F      1      2

程序:
while (<IN2>)
{chomp;
my @a=split /\t/,$_;
$hash{$a[0]}=[$a[2],$a[2]];
}

my @b=<IN1>;
chomp @b;
foreach my $c(@b)
{print "$s\t$hash{$s}";}

哈希好像用错了
作者: q1208c    时间: 2014-07-22 08:45
回复 1# 静女,其姝
  1. --- 1.pl        2014-07-22 08:44:31.381310889 +0800
  2. +++ 2.pl        2014-07-22 08:45:00.374317751 +0800
  3. @@ -1,10 +1,10 @@
  4. while (<IN2>)
  5. {chomp;
  6. my @a=split /\t/,$_;
  7. -$hash{$a[0]}=[$a[2],$a[2]];
  8. +$hash{$a[0]}=[$a[1],$a[2]];
  9. }

  10. my @b=<IN1>;
  11. chomp @b;
  12. foreach my $c(@b)
  13. -{print "$s\t$hash{$s}";}
  14. +{print "$c\t$hash{$c}";}
复制代码

作者: 萌情公方    时间: 2014-07-22 09:12
  1. #!/usr/bin/perl -w
  2. use strict;

  3. my $file_1=shift;
  4. my $file_2=shift;
  5. open(IN2,"$file_2")||die"$!\n";
  6. my %hash;
  7. while (<IN2>){
  8.         chomp;
  9.         my @a=split /\t/;
  10.         push @{$hash{$a[0]}}, ($a[1],$a[2]);
  11. }close IN2;

  12. open(IN1,"$file_1")||die"$!\n";
  13. my @b=<IN1>;
  14. chomp @b;
  15. foreach my $c(@b){
  16.         print "$c";
  17.         foreach my $s(@{$hash{$c}}){
  18.                 print "\t$s"
  19.         }
  20.         print "\n";
  21. }
  22. close IN1;
复制代码
这个数据比较简单,存数据时如果用[]就是二元数组,我记得这样的情况输出不是简单的print就行,引用学的不好,还是改成一元数组比较简单
回复 1# 静女,其姝


   
作者: 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
  1. awk 'NR==FNR{a[$1]=$0;next}{print a[$1]}' file2 file1
复制代码
这问题貌似 awk 最简单搞定呀.
作者: Monday0    时间: 2014-11-11 16:19
为什么运行不出结果来 我看代码 没看到哪有问题啊 请教大神回复 3# 萌情公方


   
作者: 清泉一边    时间: 2014-11-11 16:59
没看懂什么意思呀~输出结果不就是file2吗?
作者: Monday0    时间: 2014-11-11 20:03
不是的,我运行3楼 萌情公方 的代码,只显示C X A F,而结果是输出
C      6      5
X      2      4
A      3      2
F      1      2
可是我检查了他的代码,找不到哪里出错,大神帮帮忙
作者: 清泉一边    时间: 2014-11-12 15:53
不知道有没有猜对哦~
  1. #!/usr/bin/perl -w
  2. use strict;

  3. my $file_1=shift;
  4. my $file_2=shift;
  5. open(IN2,"$file_2")||die"$!\n";
  6. my %hash;
  7. while (<IN2>){
  8.         chomp;
  9.         my @a=split /\t/,2;
  10.         $hash{$all[0]} = $all[1];
  11. }close IN2;

  12. open(IN1,"$file_1")||die"$!\n";
  13. while (<IN1>)
  14. {
  15. chomp;
  16. print "$_\t$hash{$_}\n" if exists $hash{$_};
  17. }
  18. close IN1;
复制代码
回复 9# Monday0


   
作者: Monday0    时间: 2014-11-12 16:07
太感谢了, 我这水平很有待与提高回复 10# 清泉一边


   
作者: 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# 清泉一边


   
作者: 清泉一边    时间: 2014-11-12 17:14
有结果呀,你看看文件是不是以tab键隔开的,如果是空格隔开的,就用
  1. my @a = split /\s+/,$_,2; 再试试吧
复制代码
回复 12# Monday0


   
作者: Monday0    时间: 2014-11-12 20:25
) 真的出来了,是空格的原因回复 13# 清泉一边


   
作者: 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}}";
}






欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2