Thanks , Paul, but I think I can't make it straight part of because my
broken English , so  it seems make sense for me, but


"Paul" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
>
> --- 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

Until here, I still can't understand why have to need "='' here,
see if the value of $b is "1234"
$b=~/(\d+)/ will return "1234"
$b=~/=(\d+)/ will return "0"
then next the ($b =~ /=(\d+)/)[0] will become (0)[0] ? what hell this is ?
then I don't know what the code is going to do next.

Yeah, you must LoL again now. :) I think maybe I haven't understand the
whole thing from the first place.



>
>  ($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....




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

Reply via email to