!/usr/bin/perl
$subject={
math=>{
"mahong"=>90,
"mahao"=>98,
"zhangwei"=>89,
"yawei"=>98
},
chinese=>{
"mahong"=>90,
"mahao"=>97,
"zhangwei"=>98,
"yawei"=>100
},
};
print "Keys of subject:\n";
foreach $key (keys %{$subject}){
print $key . "\n";
}
print "Keys of math:\n";
foreach $key (keys %{$subject->{math}}){
print $key . "\n";
}
print "The best grade in math:\n";
foreach $key (keys %{$subject->{math}}){
push @tmp, $subject->{math}->{$key};
}
#求单科成绩最优
sub max{
my @tmp=@_;
$max=$_;
foreach (@tmp){
if ($max <$_){
$max = $_;
}
}
return $max;
}
$max_score=&max(@tmp);
#求单科成绩最优者
print $max_score. "\n";
foreach $key (keys %{$subject->{math}}){
if ($subject->{math}->{$key} == $max_score) {
print $key ."is good!!!\n";
}
}
$subject={
math=>{
"mahong"=>90,
"mahao"=>98,
"zhangwei"=>89,
"yawei"=>98
},
chinese=>{
"mahong"=>90,
"mahao"=>97,
"zhangwei"=>98,
"yawei"=>100
},
};
print "Keys of subject:\n";
foreach $key (keys %{$subject}){
print $key . "\n";
}
print "Keys of math:\n";
foreach $key (keys %{$subject->{math}}){
print $key . "\n";
}
print "The best grade in math:\n";
foreach $key (keys %{$subject->{math}}){
push @tmp, $subject->{math}->{$key};
}
#求单科成绩最优
sub max{
my @tmp=@_;
$max=$_;
foreach (@tmp){
if ($max <$_){
$max = $_;
}
}
return $max;
}
$max_score=&max(@tmp);
#求单科成绩最优者
print $max_score. "\n";
foreach $key (keys %{$subject->{math}}){
if ($subject->{math}->{$key} == $max_score) {
print $key ."is good!!!\n";
}
}
本文转自 tiger506 51CTO博客,原文链接:http://blog.51cto.com/tiger506/882679,如需转载请自行联系原作者