Rob -  Thanks  for the help, it was apreciated




                                                                                       
                    
                      "Rob Dixon"                                                      
                    
                      <[EMAIL PROTECTED]         To:      [EMAIL PROTECTED]             
                    
                      am.co.uk>                cc:                                     
                    
                                               Subject: Re: sorting thoughts           
                    
                      30/01/03 13:45                                                   
                    
                                                                                       
                    
                                                                                       
                    





"Steven Massey" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
m...
>
> If you could explain how this works, especially how $a $b are set with
the
> compare values
>
> my @sorted = sort {
>         (split ':', $a)[-1] <=> (split ':', $b)[-1]
>     } @array;

The block is evaluated for each pair of list elements that
'sort' needs to compare to do its job. The block must return
a value less than, equal to, or greater than zero, according
to whether $a is to be considered less than, equal to, or
greater than $b. $a and $b are implicitly set by the sort
command to the pair of list elements that it wants compared.

'split' turns a scalar into a list by splitting at the given regex.
Here we are splitting on colons. Indexing the list with [-1]
returns the last element (negative indices are relative to
the end of the list). Finally we compare the values
numerically. <=> and 'cmp' have the same effect but compare
numbers and strings respectively. They return exactly the
values that 'sort' needs: -1, 0 and +1 if the left operand is
less than , equal to, or greater the right.

Overall, sort is sorting the array in numerical order of the
last field of each element, where fields are delimited by
the colon character.

HTH,

Rob




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







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

Reply via email to