in reply to Re: Sorting strings
in thread Sorting strings
Now if I need to get an element of @array directly I use this syntax:my $array=[]; #$array is a refrence to an anonymous array my @array;
but if I want to get it through the refrence i have to do$array[1];
The -> tells perl to treat the '$array' as the SCALAR with the name 'array' and not as another type with the same name, the [] tells perl that the scalar refers to an array and not a hash. The same thing applies to the variables $_ @_ %_$array->[1];
So in the ST you wrap you data and your keys, one at a time, in anonymous arays (Notice the square brackets in the rightmost map body). Then SORT goes through each of the elements, which are refrences to arrays, and has to dereference them to get at the keys, finally the left most map takes of the anonymous array 'wrapper' and throws it away, returning the sorted list.
BTW Maybe you know this, but for map/sort/grep/join its often a good idea to read them from right to left. (Or my preference from bottom to top)
my @sorted=map {$_->[0]} sort{$a->[1] <=> $b->[1]} map {[$_,keyfunction($_)]} @list; my $nasty=join(",", map {$_->[0]} sort{$a->[1] <=> $b->[1]} map {[$_,keyfunction($_)]} grep {/^\w/} @list );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (3) Sorting strings
by demerphq (Chancellor) on Aug 31, 2001 at 06:51 UTC | |
|
Re: Re: Re: Sorting strings
by spartan (Pilgrim) on Aug 31, 2001 at 19:19 UTC | |
by demerphq (Chancellor) on Aug 31, 2001 at 22:32 UTC |