You're evaling the code on every execution of the sort block — that's terribly inefficient. Try something like this:
my $sortCriteria = '$hr->{$b}->{"number"} <=> $hr->{$a}->{"number"}'; my $sortfunc = eval "sub { $sortCriteria }'; @sortedKeys = sort $sortfunc keys %$hr;
That compiles the code just once, puts it in an anonymous function, and stores a reference to that function in $sortfunc. sort then simply calls upon this already-compiled function to compare two values.
Wrapping evaled expressions in a sub { } like that is usually advisable when you're want to run the code passed in the string in a loop.
Makeshifts last the longest.
In reply to Re: Sorting Hash: Sort Criteria Moved Into Eval
by Aristotle
in thread Sorting Hash: Sort Criteria Moved Into Eval
by mdog
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |