- 论坛徽章:
- 0
|
做了两个文件
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
代码写的挺垃圾,请大家谁帮着看一下
想了很长时间了 |
|