- 论坛徽章:
- 0
|
# An array of references to anonymous hashes
my @employees = (
{ FIRST =>'Bill', LAST =>'Gates',
SALARY =>600000, AGE =>45 },
{ FIRST =>'George', LAST =>'Tester',
SALARY =>55000, AGE =>29 },
{ FIRST =>'Steve', LAST =>'Ballmer',
SALARY =>580000, AGE =>41 },
{ FIRST =>'Sally', LAST =>'Developer',
SALARY =>50000, AGE =>29 },
{ FIRST =>'Joe', LAST =>'Tester',
SALARY =>40000, AGE =>29 },
);
sub seniority {
$b->{SALARY} <=> $a->{SALARY}
or $b->{AGE} <=> $a->{AGE}
or $a->{LAST} cmp $b->{LAST}
or $a->{FIRST} cmp $b->{FIRST}
}
my @ranked = sort seniority @employees;
foreach my $emp (@ranked) {
print "$emp->{SALARY}\t$emp->{AGE}\t$emp->{FIRST}\t$emp->{LAST}\n";
}
------------------------------------------------------------------------
Result:
600000 45 Bill Gates
580000 41 Steve Ballmer
55000 29 George Tester
50000 29 Sally Developer
40000 29 Joe Tester
-----------------------------------------------
程序段
sub seniority {
$b->{SALARY} <=> $a->{SALARY} #1
or $b->{AGE} <=> $a->{AGE} #2
or $a->{LAST} cmp $b->{LAST} #3
or $a->{FIRST} cmp $b->{FIRST} #4
}
其中#1执行后结果应该为“真”阿,“or“后,#2,#3,#4应该都不会执行的呀!为何Result中还显示了#2,#3,#4的结果?
大大们救命阿! |
|