in reply to sorting an array of an arrays

Dear Monks--

Not homework!

I'm a bookseller who's straying into the realm of web-building and perl programming. (www.amherstbooks.com is all mine, for better or worse.) I did spend a lot of time looking at some of the offered resources and still found it difficult to figure out how to do it.
Thank you all for your help!

Nat

Replies are listed 'Best First'.
Re^2: sorting an array of an arrays
by ysth (Canon) on Aug 13, 2006 at 23:34 UTC
    Your description of what you wanted was pretty good:
    I'd like to sort it by $Institution and within that sort by $CourseNumber (and possibly other values)
    You just needed to break it down into pieces that perl can understand. "sort it by" you correctly translated as $$a[n] cmp $$b[n], but you left out the more difficult-to-translate "within that". It helps to ask yourself "what does this really mean, on a level that perl could understand?", the answer, in this case, being "if the institutions are different, sort by them, otherwise, sort by course number". Which leads pretty directly to the solution proposed to you. Extending it to handle the "and possibly other values" should indicate a loop to you, something like:
    sort { my $compare; for (my $i = 0; !$compare && $i < @$a; ++$i) { $compare = $a->[$i] cmp $b->[$i]; } return $compare; }
Re^2: sorting an array of an arrays
by aufflick (Deacon) on Aug 14, 2006 at 08:54 UTC
    My apologies then, and enjoy the excellent answers from Tanktalus et al. Good on you for building your own site - I hope you are able to get to where you want to be with the help of Perlmonks!

    PS: I haven't been to MA for about 6 years, but I miss visiting Boston and the rest of New England - next time I come I'll try and pop in to your shop :)