in reply to Advanced Sorting
You need the "no strict 'refs'" because thatmy $field = "name"; sub numsort { $a->{$field} <=> $b->{$field} } sub alphasort { $a->{$field} cmp $b->{$field} } my @data = ( { name => 'jim', age => 16 }, { name => 'bob', age => 18 }, ); my $sortsub = $field eq "name" ? 'alphasort' : 'numsort'; my @sorted; { no strict 'refs'; @sorted = sort $sortsub @data; }
is actually using symbolic references, which is ugliness itself. :)sort $sortsub @data
Can sort subs be sub references? From the docs I looked at, it doesn't seem like they can. If they could, that would be a *much* cleaner way of doing it.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: Advanced Sorting
by chromatic (Archbishop) on Apr 20, 2000 at 02:53 UTC |