in reply to Re: wisdom needed
in thread wisdom needed: sorting an array

The principal of counting frequency is based on this type of code:

my @array = (1,1,1,2,3,3,4,4,4,4); my %count; $count{$_}++ for @array; my @ordered = sort {$count{$b} <=> $count{$a}} keys %count; print map {"$_\n"} @ordered;

The hash stores the count in the value and the item in the key. The keys are then sorted by comparing the values with eachother, with respect to the keys.

--
Steve Marvell

Replies are listed 'Best First'.
Re: Re: Re: wisdom needed
by Anonymous Monk on Jun 06, 2002 at 14:15 UTC
    thanks marvell, this runs well, but the only problem is that the output prints every number (i only want one of each number printed) it also prints every number again when it comes across a new number!! e.g
    @array = (1,1,1,1,2,2,3,3,3,4,4,4,5,5) OUTPUT 1 1 1 1 2 1 2 1 3 2 1 3 2 1 ETC
    I am also not sure if is printing the most frequent value at the top or not. any suggestions??? :-) the help so far is much appreciated.