--- EriK W <[EMAIL PROTECTED]> wrote:
> Hi guys,
> 
> I have a question maybe about "sort" or regular expression,
> 
> @new = sort {($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0]|| uc($a) 
> cmp uc($b) } @old;
> 
> I don't really understand this /=(\d+)/ [0] thing, expecially that
> "=" , what does that mean?

Let's break it down! =o)

 @new =  sort { 
      #  . . . sort can use an anonymous subrouting to determine order
      } @old;
  
That much makes sense, I assume?

The value returned by sort()'s compare routine should return 0 if the
values are equal, less than zero to put the first value first, or more
than zero to put the second value first. (Check me, list. =o)

This is commonly accomplished with Perl's string comparator (cmp) or
numeric comparator (<=>).

So, let's look at the comparison expression.

 ($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0] 
                     ||
              uc($a) cmp uc($b) 
      
This is a numeric compare, which returns on a true (any nonzero,
whether less or greater than zero). If <=> returns zero for equality,
the || tests the cmp operator and returns it's value. In

 ($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0] 

the <=> is comparing the first mathed number in the second value to the
first matched number in the first value. That's because

 $b =~ /=(\d+)/

Looks at $b (the default second argument) and matches one or more
digits following an equal sign -- the = is a literal character to match
here, it's not doing anything magical. Putting parens around it and
subscripting

 ($b =~ /=(\d+)/)[0]

means treat the resulting list of matches as an array, and give me
element zero. Thus, it's getting the first matched set of digits in $b
that follow an equal sign. The rest of the subexpression

 ($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0] 

does the same thing to $a, and numerically compares the numbers
returned with <=>. IFF they are equal <=> will return a 0 which perl
will interpret as a boolean FALSE, and the next section of the
expression will be evaluated by the ||

 uc($a) cmp uc($b) 

This just uppercases both arguments (to effectively do a simple
case-insensitive comparison, since case would otherwise matter). Thus
the end result of the compare subrouting says:

   1) find the first set of numerals following an equal sign
      in both $a and $b and compare them; 
      if $b's number is higher, put it first;
      if $a's number is higher, put it first;
      if they are the same, THEN....
   2) do a case insensitive comparison of $a and $b;
      if $a is lower, put it first;
      if $b is lower, put it first.

I don't know the behavior for certain on a final return of zero, but I
don't think it does anything, which likely means that later things may
or may not reverse their order based on comparisons of other pairs, so
that the end result is unpredictable. 

Comments?

Surely in that much tyoing I botched something, lol....


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to