免费注册 查看新帖 |

Chinaunix

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

关于散列读取文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-27 11:09 |只看该作者 |正序浏览
做了两个文件

cat chengji.log
id chengji
1 88
2 90
3 85
4 70
5 90
6 99
7 85
8 65
9 70
10 66

现在想用散列按照成绩排序,取出前五名的id
出现的问题是hash对文件读取的时候返回keys 和values 不是我想要的结果
代码如下
##########################
#!/usr/bin/perl
open(OPEN,"chengji.log");
%hash_old=<OPEN>;
chomp(%hash_old);
foreach $key (keys %hash_old){
        #$keys=keys %hash_old;
        $value=$hash_old {$key};
        print "the value is $value \n";
        print "the id is $key \n";
}
#######################
返回结果
the value is 5 90
the id is 4 70

the value is 1 88 90
the id is id chengji

the value is  
the id is 10 66

the value is 9 70
the id is 8 65

the value is 7 85
the id is 6 99

the value is 3 85
the id is 2 90

第二段代码如下
###########################
#!/usr/bin/perl
open(OPEN,"chengji.log");
@OPEN=<OPEN>;
chomp @OPEN;
foreach $chengji (@OPEN){
$chengji="$chengji ";
push(@array,$chengji);
        print "$chengji \n";
}
%HASH=@array;
chomp %HASH;
foreach $key (keys %HASH){
        $value=$HASH{$key};
        print "the value is $value\n";
        print "the id is $key\n";
}
#######################
结果为:
id chengji  
1 88
2 90  
3 85  
4 70  
5 90  
6 99  
7 85  
8 65  
9 70  
10 66  
the value is 5 90
the id is 4 70
the value is 1 88
the id is id chengji
the value is 3 85
the id is 2 90
the value is 7 85
the id is 6 99
the value is
the id is 10 66
the value is 9 70
the id is 8 65


代码写的挺垃圾,请大家谁帮着看一下
想了很长时间了

论坛徽章:
0
30 [报告]
发表于 2008-08-28 20:07 |只看该作者

回复 #29 hitsubunnu 的帖子

这fuck语写得好长啊

论坛徽章:
0
29 [报告]
发表于 2008-08-28 17:21 |只看该作者
say "my special long is fuck do computer"


俺の特長はパソコンをファックすることだ

论坛徽章:
0
28 [报告]
发表于 2008-08-28 17:07 |只看该作者
都挺牛b的,呵呵

论坛徽章:
0
27 [报告]
发表于 2008-08-28 16:57 |只看该作者
my special long is fuck do computer


easyworld 该用户已被删除
26 [报告]
发表于 2008-08-28 15:57 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
25 [报告]
发表于 2008-08-28 15:43 |只看该作者
原帖由 hitsubunnu 于 2008-8-28 15:36 发表


print sort {(split(/ /,$b))[1]  (split(/ /,$a))[1]} ;

__END__
1 88
2 90
3 85
4 70
5 90
6 99
7 85
8 65
9 70
10 66





6 99
2 90
5 90
1 88
3 85
7 85
4 70
9 70
10 6 ...

甚为拉风

论坛徽章:
0
24 [报告]
发表于 2008-08-28 15:36 |只看该作者


  1. print sort {(split(/ /,$b))[1] <=> (split(/ /,$a))[1]} <DATA>;

  2. __END__
  3. 1 88
  4. 2 90
  5. 3 85
  6. 4 70
  7. 5 90
  8. 6 99
  9. 7 85
  10. 8 65
  11. 9 70
  12. 10 66

复制代码



  1. 6 99
  2. 2 90
  3. 5 90
  4. 1 88
  5. 3 85
  6. 7 85
  7. 4 70
  8. 9 70
  9. 10 66
  10. 8 65

复制代码

论坛徽章:
0
23 [报告]
发表于 2008-08-28 14:41 |只看该作者
原帖由 doiob 于 2008-8-28 14:05 发表
感觉lz和那位面试时say "my special long is fuck do computer"的高人有一拼。


差点没看懂,你怎么听懂的。。。。

论坛徽章:
0
22 [报告]
发表于 2008-08-28 14:05 |只看该作者
感觉lz和那位面试时say "my special long is fuck do computer"的高人有一拼。
  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP