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

[MacPerl] sorting; difference in Mac/Unix



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.) I hope someone can give a detailed explaination.

What the following script does is to generate an array of array, called @yearMonthArray. 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

1997, 12
1998, 10
1998, 9
1998, 8
1998, 7
1998, 6
1998, 5
1998, 4
1998, 3
1998, 2
1998, 1
1997, 11


In Linux, I got

1998, 10
1998, 9
1998, 8
1998, 7
1998, 6
1998, 5
1998, 4
1998, 3
1998, 2
1998, 1
1997, 12
1997, 11

Would you experts explain?

-------------------------------------------

#!/usr/local/bin/perl -w

# an example 2 dimentional array. Xah

use strict;

# &yearMonthRange(startYear,startMonth,endYear,endMonth) returns an array that is a range from the start to end year/month. The elements are addresses to arrays. start year/month must be smaller than end year/month. No error checking on input. Example: yearMonthRange(1997,11,1998,3) returns ([1997,11],[1997,12],[1998,1],[1998,2],[1998,3]).
# testing:
# my(@a3) = yearMonthRange(1997,11,1998,3); for (my($i) = 0; $i <= $#a3; $i++) {print "${@{$a3[$i]}}[0], ${@{$a3[$i]}}[1]\n";};

sub yearMonthRange {
    my($startY,$startM,$endY,$endM) = @_;

    my($yyyy,$mm) = ($startY,$startM);
    my(@result) = [$yyyy,$mm];
    while (($yyyy < $endY) or (($yyyy == $endY) and ($mm < $endM))) {
        if ($mm == 12) {$yyyy++; $mm = 1} else {$mm++};
        push (@result,[$yyyy,$mm]);
    };
    return @result;
}

my(@yearMonthArray) = yearMonthRange(1997,11,1998,10);

# my(@newA)= @yearMonthArray;
 my(@newA)= sort {$b <=> $a} @yearMonthArray;
# my(@newA) = sort { ${@{$b}}[0] <=> ${@{$a}}[0] || ${@{$b}}[1] <=> ${@{$a}}[1]} @yearMonthArray;

foreach $_ (@newA) {
    my($year1,$month1) = (${@{$_}}[0],${@{$_}}[1]);
print "$year1, $month1\n"
}


 Xah, xah@best.com
 http://www.best.com/~xah/PageTwo_dir/more.html
 Unix + C = source and inspiration of all bugs




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