免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5704 | 回复: 14
打印 上一主题 下一主题

哈希求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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}";}

哈希好像用错了

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
2 [报告]
发表于 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}";}
复制代码

论坛徽章:
0
3 [报告]
发表于 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# 静女,其姝


   

论坛徽章:
0
4 [报告]
发表于 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;

论坛徽章:
0
5 [报告]
发表于 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;   这个比较简单。

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
6 [报告]
发表于 2014-07-23 10:33 |只看该作者
  1. awk 'NR==FNR{a[$1]=$0;next}{print a[$1]}' file2 file1
复制代码
这问题貌似 awk 最简单搞定呀.

论坛徽章:
0
7 [报告]
发表于 2014-11-11 16:19 |只看该作者
为什么运行不出结果来 我看代码 没看到哪有问题啊 请教大神回复 3# 萌情公方


   

论坛徽章:
1
羊年新春福章
日期:2015-04-28 20:40:58
8 [报告]
发表于 2014-11-11 16:59 |只看该作者
没看懂什么意思呀~输出结果不就是file2吗?

论坛徽章:
0
9 [报告]
发表于 2014-11-11 20:03 |只看该作者
不是的,我运行3楼 萌情公方 的代码,只显示C X A F,而结果是输出
C      6      5
X      2      4
A      3      2
F      1      2
可是我检查了他的代码,找不到哪里出错,大神帮帮忙

论坛徽章:
1
羊年新春福章
日期:2015-04-28 20:40:58
10 [报告]
发表于 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


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP