Chinaunix

标题: 关于散列读取文件 [打印本页]

作者: ccie307    时间: 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


代码写的挺垃圾,请大家谁帮着看一下
想了很长时间了
作者: churchmice    时间: 2008-08-27 12:24

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

这种牛逼的写法谁教你的?
作者: cobrawgl    时间: 2008-08-27 12:32
%hash_old=<OPEN>;  这么写倒是有创意
chomp(%hash_old);     这么写倒是挺NB

lz 好好看看 learning perl 去吧
作者: churchmice    时间: 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])
复制代码

作者: flw    时间: 2008-08-27 12:37
split /\s+/;
通常都写成
split
作者: khandielas    时间: 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
作者: ly5066113    时间: 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个。
作者: ccie307    时间: 2008-08-27 13:18
标题: 回复 #3 cobrawgl 的帖子
hash哪个部分看learningperl很多遍了
估计还是没有参透

你说的收到了
作者: forlorngenius    时间: 2008-08-27 13:26
呵呵,lz倒是创意很多。呵呵,perl书看起来要细心
作者: churchmice    时间: 2008-08-27 16:12
标题: 回复 #5 flw 的帖子
受教了
又可以少码些字了
ps:水木perl版版主也是你?
作者: flw    时间: 2008-08-27 16:16
原帖由 churchmice 于 2008-8-27 16:12 发表
受教了
又可以少码些字了
ps:水木perl版版主也是你?

现在已经下台了。
作者: ccie307    时间: 2008-08-27 17:34
看了半个下午了
my $log= shift@ARGV||"chengji.log";
open my $file,"<",$log or die "Fail to open $log $!";
这两句啥意思啊
||不是短路符吗
怎么可能把 chengji.log放到@ARGV里面呢?
作者: forlorngenius    时间: 2008-08-27 17:39
标题: 回复 #12 ccie307 的帖子
my $log= shift@ARGV||"chengji.log";


是shift @ARGV 这个不成功才用  chengji.log
作者: ccie307    时间: 2008-08-27 17:44
哦  那我就理解对了 他是短路字符
那  文件是通过
open my $file,"<",$log or die "Fail to open $log $!";

这句来读取的话?

shift @AGRV能读出啥呢

不好意思哈,基础太差
但热情足够  
作者: ccie307    时间: 2008-08-27 17:48
The array @ARGV contains the command-line arguments intended for the script. $#ARGV is generally the number of arguments minus one, because $ARGV[0] is the first argument, not the program's command name itself. See $0 for the command name.

好像明白了  谢谢大家了
作者: churchmice    时间: 2008-08-27 19:32
标题: 回复 #11 flw 的帖子
原来这样啊
我前几天上水木发现居然有个perl版所以赶忙申请了个ID
看到版主很熟悉
作者: cobrawgl    时间: 2008-08-27 20:01
原帖由 churchmice 于 2008-8-27 19:32 发表
原来这样啊
我前几天上水木发现居然有个perl版所以赶忙申请了个ID
看到版主很熟悉



除了这里,好像也没有别的地方可以混混了吧
作者: forlorngenius    时间: 2008-08-27 20:10

作者: churchmice    时间: 2008-08-27 20:48
标题: 回复 #17 cobrawgl 的帖子
确实
以前我们学校还有个perl版
后来改版成complang了
作者: ccie307    时间: 2008-08-28 00:41
又仔细看了一下发现 下面的语法在learningperl中都有提到
看书不细
看来真得看上一百遍啊一百遍
my $log= shift@ARGV||"chengji.log";
open my $file,"<",$log or die "Fail to open $log $!";
作者: khandielas    时间: 2008-08-28 01:06
原帖由 ly5066113 于 2008-8-27 13:15 发表


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


是,忘了score可以不是unique的, 需要象churchmice那样sort value

#!/usr/bin/env perl
use strict;

local $/;
$_ = <>;
s/[A-Za-z]+//g;
my %chengji = split;
print "$_ $chengji{$_}\n" foreach ( ( sort { $chengji{$b}<=>$chengji{$a} } keys %chengji)[0..4] );

./test_hash.pl chengji.log
6 99
2 90
5 90
1 88
3 85
作者: doiob    时间: 2008-08-28 14:05
感觉lz和那位面试时say "my special long is fuck do computer"的高人有一拼。
作者: sheenshine    时间: 2008-08-28 14:41
原帖由 doiob 于 2008-8-28 14:05 发表
感觉lz和那位面试时say "my special long is fuck do computer"的高人有一拼。


差点没看懂,你怎么听懂的。。。。
作者: hitsubunnu    时间: 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

复制代码

作者: churchmice    时间: 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 ...

甚为拉风
作者: easyworld    时间: 2008-08-28 15:57
提示: 作者被禁止或删除 内容自动屏蔽
作者: cobrawgl    时间: 2008-08-28 16:57
my special long is fuck do computer



作者: xiaoyu9805119    时间: 2008-08-28 17:07
都挺牛b的,呵呵
作者: hitsubunnu    时间: 2008-08-28 17:21
say "my special long is fuck do computer"


俺の特長はパソコンをファックすることだ
作者: forlorngenius    时间: 2008-08-28 20:07
标题: 回复 #29 hitsubunnu 的帖子
这fuck语写得好长啊




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