in reply to Sorting Hash: Sort Criteria Moved Into Eval

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.

Replies are listed 'Best First'.
Re^2: Sorting Hash: Sort Criteria Moved Into Eval
by aanhouden (Novice) on Jul 07, 2009 at 11:39 UTC
    Thanks aristotle for your exemple. It got me to understand much better eval with sort. Here is how I solved my problem. I wanted to sort on column 0, then 1, then 2... variably depending on user input. Here is my test code maybe it will help others.
    push my @tableau,[222,234523676,26256,5645,634,56,3456,5345623]; push @tableau,[333,2346,2356457,347,7745,67,3462]; push @tableau,[3.3,2341,2356457,347,7745,67,3462]; push @tableau,[333,2349,2356457,347,7745,67,3462]; push @tableau,[777,67567,4568458,45845,48458,4578]; push @tableau,[999,67567,4568458,45845,48458,4578]; push @tableau,[444,67567,4568458,45845,48458,4578]; push @tableau,[111,67567,4568458,45845,48458,4578]; my $grandeur; foreach (@tableau){ my $compteur = 0 unless $compteur; $grandeur = $#{$tableau[$compteur]} if ($grandeur < $#{$tableau[$c +ompteur]}); $compteur++; } my @sorter; foreach (0..$grandeur){ my $sortCriteria = "\$tableau[\$b][$_] <=> \$tableau[\$a][$_]"; push @sorter,$sortCriteria; } $sorter = join " or ",@sorter; print "$sorter\n"; my $sortfunc = eval "sub { $sorter }"; @sortedKeys = sort $sortfunc 0..$#tableau; print "@{$tableau[$_]}\n" foreach @sortedKeys;
    Good luck.