On Mon May 01 14:14:00 2017, JPIERCE wrote:
Show quoted text> Will return most common value(s), and unlike the mode in
> Statistics::Descriptive and Statistics::Basic, works with text as well
> as numbers.
>
> sub mode{
> my %F;
> $F{$_}++ foreach @_;
> my $max = $F{ (sort { $F{$b}<=>$F{$a} } keys %F)[0] };
> grep { $F{$_}==$max } keys %F;
> }
Slightly less elegant, but more robust:
sub mode{
my %F;
$F{$_}++ foreach grep { defined } @_;
my $max = $F{ (sort { $F{$b}<=>$F{$a} } keys %F)[0] };
my @modes = grep { $F{$_}==$max } keys %F;
return wantarray() ? @modes : (scalar @modes > 1 ? undef : $modes[0]);
}
This will return undef in scalar context if more than one mode exists. The previous version would permit undef as a valid data point/mode, but in scalar context you'd end up with the number of modes for your data rather than the actual mode, even for a unimodal data set.
Alternatively, keep the previous version and simply note in documentation that it must always be called in list context....