Statis...c::Mode(3pm)
π Statistics::Basic::Mode π
okay bestie, gather round, because we're talking about Statistics::Basic::Mode β that's the Statis...c::Mode(3pm) page from the User Contributed Perl Documentation, and yes the header really does abbreviate itself like that, iconic honestly. she finds the mode of a list. that's her whole thing. her one job. her moment. π
β¨ SYNOPSIS β¨
so you can summon her like this:
my $mode = mode(1,2,3,3);
or, if you're feeling vector-coded today:
my $v1 = vector(1,2,3,3);
my $mod = mode($v1);
and then you either query the values or just print them, like:
print "The mod of $v1: $mod\n";
my $mq = $mod->query;
my $m0 = 0+$mod; # this will croak occasionally, see below
π¨ heads up bestie: that last line, 0+$mod? she will croak sometimes. that's not a drill. keep reading, we get into it.
here's the tea though β the mode of an array is not necessarily a scalar. sometimes the mode of a vector is a whole entire vector. it's giving overachiever:
my $mod = mode(1,2,3);
my $v2 = $mod->query;
print "hrm, there's three elements in this mode: $mod\n"
if $mod->is_multimodal;
and if you wanna build a 20 point "moving" mode β the kind that chugs along a database stream and recalculates as you go β do it like this:
use Statistics::Basic qw(:all nofill);
my $sth = $dbh->prepare("select col1 from data where something");
my $len = 20;
my $mod = mode()->set_size($len);
$sth->execute or die $dbh->errstr;
$sth->bind_columns( my $val ) or die $dbh->errstr;
while( $sth->fetch ) {
$mod->insert( $val );
if( defined( my $m = $mod->query ) ) {
print "Mode: $m\n";
}
print "Mode: $mod\n" if $mod->query_filled;
}
look at her go. running mode. living her best life. slay. π
π METHODS π
new()
the constructor. she takes a list of values, a single array ref, or a single Statistics::Basic::Vector as arguments. and she hands you back a Statistics::Basic::Mode object. that's it, that's the deal. π«Ά
note: normally you'd use themean()constructor, rather than building these by hand usingnew(). (yes, the original saysmean()there. we don't ask questions, we just report the facts, no cap.)
is_multimodal()
so Statistics::Basic::Mode objects sometimes hand back Statistics::Basic::Vector objects instead of numbers. rude? no. correct? yes. when is_multimodal() is true, the mode is a vector, not a scalar. that's the whole vibe check right there.
_OVB::import()
this module also inherits all the overloads and methods from Statistics::Basic::_OneVectorBase. so she's not just a mode girlie, she's got family. π
π₯ OVERLOADS π₯
this object is overloaded, bestie. she tries to return an appropriate string for the calculation, or the value of the computation when you're in numeric context.
in boolean context, this object is always true β even when empty. always. even when there's nothing in there. main character energy fr fr. π
if you evaluate her as a string, Statistics::Basic::Mode will try to format a number (like any other Statistics::Basic object), but if the object is_multimodal(), she'll instead return a Statistics::Basic::Vector for stringification. the plot twist:
$x = mode(1,2,3);
$y = mode(1,2,2);
print "$x, $y\n"; # prints: [1, 2, 3], 2
see?? [1, 2, 3], 2. one of them said "actually I contain multitudes" and the other one was just a normal number minding its business. π
and if you evaluate her as a number? a Statistics::Basic::Mode will raise an error when the object is_multimodal(). so. don't do that. or do, but know what you're getting into. π
π¦ AUTHOR π¦
Paul Miller <jettero@cpan.org> π
πΈ COPYRIGHT πΈ
Copyright 2012 Paul Miller -- Licensed under the LGPL
β SEE ALSO β
perl(1)Statistics::BasicStatistics::Basic::_OneVectorBaseStatistics::Basic::Vector
and that's the whole page, bestie!! π this was pulled from perl v5.36.0, dated 2022-11-19, under the <rainbow>Statis...c::Mode(3pm)</rainbow> banner β so if you ever see that truncated little header floating around, you know exactly who she is. go find some modes. be multimodal about it. periodt. πβ¨
π¦ where this page comes from
- debian bookworm: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- debian trixie: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- ubuntu noble: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- ubuntu resolute: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- ubuntu stonking: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms