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

Re: [MacPerl] sorting; difference in Mac/Unix



Xah Lee <xah@best.com> writes:

> The following is something interesting I discovered. I think the gist is that
> the order of addresses in arrays of arrays are different in Unix perl and
> MacPerl. (Correct me if I'm wrong.)

You are technically right, but your choice of words is not quite correct: There
are no arrays of *arrays*, but arrays of array *references*.

>What the following script does is to generate an array of array, called @yearM
onthArray. Essentially, @yearMonthArray = ([1997,11],[1997,12],[1998,1],[1998,2
],[1998,3],...).
>
>Now the script sort it using
>
> my(@newA) = sort {$b <=> $a} @yearMonthArray;
>
> The output of the script is always the same on Mac or Unix, but the two
> differs. In MacPerl, I got

When sort is sorting an array of references the way you do it, $a and $b
evaluate to the memory address of the array reference when evaluated (for a
demonstration, try

   print [], "\n";
   print []+0, "\n";

The order in which nontrivial data structures get allocated in memory is
undefined, and MacPerl indeed has a tendency for some sorts of allocations to
alloct "backwards" in memory.

Matthias

-- 
Matthias Neeracher   <neeri@iis.ee.ethz.ch>   http://www.iis.ee.ethz.ch/~neeri
   "One fine day in my odd past..." -- Pixies, _Planet of Sound_

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