Paul Johnson wrote:

<snip Mark's code>

> But don't go relying on the ordering of the array.  Hashes don't
> preserve order.  If you need an ordering, impose it.  eg
> 
>   print join "\n", sort @ary;

Should we get into a thread on 'sort' ~8^) ?

I thought I'd throw this in there (in case some of you get adventurous
and modify the data).  Note that the standard behavior of 'sort' is to
sort case-SENSITIVE alphabetically.  To sort case-INsensitively

   print join "\n", sort{ lc( $a ) cmp lc( $b ) } @ary;

The sort function can take an anonymous block or a routine to tell it
how to sort (an anonymous block was used above).  This block says "use
the lowercase versions of the elements when comparing".  Doing the same
thing with a home-spun routine:

   sub caseInsensitive {
      lc( $a ) cmp lc( $b );
   }

Then the 'sort' would look like

   print join "\n", sort caseInsensitive @ary;    

To sort numerically, use

   sort{ $a <=> $b } @ary

There are a lot more things that can be done with sort....Yeah, I
probably shoulda kept my mouth shut ~8^)

Dan

Reply via email to