On Wed, Dec 05, 2001 at 10:20:45PM +0000, Richard Smith wrote: > I have a list of Referrer names with a frequency > seperated by a space, as follows: > > "http://www.foo.com 140" > "http://www.bar.com 10" > "http://www.foo.com/faux 1087" > > I want to sort this list by the frequency, and by the > URL, so that I get: > > "http://www.foo.com/faux 1087" > "http://www.foo.com 140" > "http://www.bar.com 10" > > I have tried the following script, which doesn't work > as I expected, and now I am stuck. Can anyone point me > to the right solution? Any help is much appreciated.
Your code is very close... > __SNIP__ > @sort_refs = sort { &count($a) <=> &count($b) or You want descending order for the numeric comparison, so switch $a and $b. > &host($a) cmp &host($b) } @refs; > > sub count { > shift; shift does not assign to $_ by default; you would need to do $_ = shift;. > /\s+(\d+)/; > return $1; > } However, the assignment to $_ isn't necessary to do the pattern match. You can use the =~ operator to match against any value. Additionally, you should probably make sure the match succeeded, just in case there's some unexpected input. sub count { if ($_[0] =~ /\s+(\d+)/) { return $1; } else { return 0; } } That should get your code working. There are ways to write the sort so that it is more efficient, by avoiding all the subroutine calls that your code will make. If you'd like to know more just say so! Ronald