|
Posted by DJ Stunks on December 10, 2006, 12:22 pm
Please log in for more thread options
harryfmudd [AT] comcast [DOT] net wrote:
> John W. Krahn wrote:
> > DJ Stunks wrote:
> >>for each row I'd like to return the column
> >>numbers which have the highest number of counts, and the highest column
> >>number with any counts at all. I refer to these values as mode and max
> >>respectively
> >>
> >> print "id\tmode\tmax\n";
> >> while ( my $hashref = fetchrow_hashref() ) {
> >> my $id = $hashref->;
> >>
> >> # mode = col number with greatest count
> >> my $mode = ( map { $_->[0] }
> >> sort { $b->[1] <=> $a->[1] }
> >> map { [ $_, $hashref-> ] } 1..5 )[0];
> >>
> >> # max = highest col number with any count
> >> my $max = ( sort { $b <=> $a }
> >> map { $_ if $hashref-> > 0 } 1..5 )[0];
> >>
> >> $max = 1 if $max eq ''; #all zeros
> >
> >
> >
> > If you want efficiency then you should just use loops instead of sorting:
> >
> >
> > my ( $mode, $curr ) = ( 1, 0 );
> > for ( 1 .. 5 ) {
> > if ( $hashref->{ "col$_" } > $curr ) {
> > $curr = $hashref->{ "col$_" };
> > $mode = $_;
> > }
> > }
> >
> > my $max = 1;
> > for ( reverse 1 .. 5 ) {
> > if ( $hashref->{ "col$_" } > 0 ) {
> > $max = $_;
> > last;
> > }
> > }
> >
>
> Have you looked at List::Util? It's a standard module in Perl 5.8, and
> contains a max function. So the first loop becomes something like
>
> use List::Util qw;
>
> my $mode = max (map } 1..5);
>
> Assuming the only zeros are trailing, the second loop could be
>
> my $max = grep {$_ > 0} map } 1..5
I appreciate the input, I don't think I was clear enough about what I
was looking for. The counts themselves don't really matter, what I was
looking for was the column numbers associated with 1) highest count
($mode); and 2) the highest column number with any count at all ($max).
John's solution worked well. I was only thinking of sort() because I
wanted largest of one thing with respect to another thing. I couldn't
think of a way to get max() to operate like that without going through
all the columns twice (once to find the max, and a second time to find
which column it was associated with).
Thanks,
-jp
|