[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] efficiency & memory



Mark Manning/Muniz Eng. writes:
|That is very strange.  :-)  I had never heard of that before.  What I
|thought was happening was that it would sort the numbers between zero
|and $#id.  Which meant that they would go 0, 1, 2, 3, etc....

If it was a simple sort 0..$#ids, that would be the case. However, it
was sort byID 0..$#ids, so the byID sub is free to determine the order
in whatever way is desired. For example:

@ids = ([1, 'b'], [2, 'a'], [0, 'c']);

@ids = @ids[sort {$ids[$a][0] <=> $ids[$b][0]} 0..$#ids];

(or something moderately similar) sorts @ids by the first element of
each of the hashes referenced by @ids. Of course, something like

@ids = sort {$a->[0] <=> $b->[0]} @ids;

is clearer and more efficient in this case, but the sub can be arbitrary
complex and there may be no simple or memory efficient way to get a list
of the elements to be sorted (as opposed to id numbers of the elements to
be sorted).

On the other hand, if the sub is too complex, a Schwartzian transform
might be the way to go, if the number of elements or the information needed
to compare two elements is small.

Not that this has anything to do with MacPerl or the original question...

Brian

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch