免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 6371 | 回复: 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
2 [报告]
发表于 2008-08-27 12:24 |只看该作者

  1. %hash_old=<OPEN>;
复制代码

这种牛逼的写法谁教你的?

论坛徽章:
0
3 [报告]
发表于 2008-08-27 12:32 |只看该作者
%hash_old=<OPEN>;  这么写倒是有创意
chomp(%hash_old);     这么写倒是挺NB

lz 好好看看 learning perl 去吧

论坛徽章:
0
4 [报告]
发表于 2008-08-27 12:36 |只看该作者

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $log = shift @ARGV || "chengji.log";
  5. open my $file,"<",$log or die "Fail to open $log $!";
  6. my %score;
  7. while (<$file>){
  8.         next if /^(id chengji)/;
  9.         my ($id,$score) = split /\s+/;
  10.         $score{$id} = $score;
  11. }
  12. print "$_ score $score{$_}\n" foreach ( ( sort { $score{$b} <=> $score{$a} } keys %score)[0..4])
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2008-08-27 12:37 |只看该作者
split /\s+/;
通常都写成
split

论坛徽章:
0
6 [报告]
发表于 2008-08-27 12:50 |只看该作者
#!/usr/bin/env perl
use strict;
use warnings;

open CHENGJI, 'chengji.log';
local $/;
$_ = <CHENGJI>;
s/[A-Za-z]+//g;
my %chengji = reverse split;

my @k = reverse sort {$a<=>$b} keys %chengji;
foreach (0..4) {
   print "$chengji{$k[$_]} $k[$_]\n";
}

打印结果
6 99
2 90
1 88
3 85
4 70

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
7 [报告]
发表于 2008-08-27 13:15 |只看该作者
原帖由 khandielas 于 2008-8-27 12:50 发表
#!/usr/bin/env perl
use strict;
use warnings;

open CHENGJI, 'chengji.log';
local $/;
$_ = ;
s/[A-Za-z]+//g;
my %chengji = reverse split;

my @k = reverse sort {$a$b} keys %chengji;
fo ...


很明显不对,90分的有2个。

论坛徽章:
0
8 [报告]
发表于 2008-08-27 13:18 |只看该作者

回复 #3 cobrawgl 的帖子

hash哪个部分看learningperl很多遍了
估计还是没有参透

你说的收到了

论坛徽章:
0
9 [报告]
发表于 2008-08-27 13:26 |只看该作者
呵呵,lz倒是创意很多。呵呵,perl书看起来要细心

论坛徽章:
0
10 [报告]
发表于 2008-08-27 16:12 |只看该作者

回复 #5 flw 的帖子

受教了
又可以少码些字了
ps:水木perl版版主也是你?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP