in reply to finding the most popular in array

$hash{$_}++ foreach @array; foreach keys %hash { $max = $_ if $hash{$_} > $hash{$max} }; print $max, "\n";

Replies are listed 'Best First'.
Re: Re: finding the most popular in array
by clintp (Curate) on Sep 10, 2001 at 07:29 UTC
    I liked your way of doing it, since it's two passes and the original question only wanted the most popular. Much better than sorting.

    The other day I caught myself coding this same thing (boiling down a larger problem) but with this variation:

    $hash{$_}++ for @array; %hash=reverse %hash; for (keys %hash) { $max = $_ > $max ? $_ : $max }; print $hash{$max}, "\n";
    Thank goodness for TIMTOWDI.
      It could be done in one loop too.
      @a= qw(a b c d e c d e c d e c d e c d e a b c d a b c d a b c d a b c + d a b a b a e e d a a a a a a a a); for(@a) { if ($c<$r{$_}++) { $m=$_; $c=$r{$_} } }
      And this is to check.
      for (keys %r){print "$_ $r{$_}\n"}; print "\n\$m is $m\n\$c is $c";
      Hopes
      I hope you had enjoyed it