Statis...:Median(3pm)

πŸ“¦
version 1.6611 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ Statistics::Basic::Median ✨

okay bestie, gather round πŸ’– this is the Statistics::Basic::Median page from the Statis...:Median(3pm) manual (yes it's a perl thing, section 3pm, user contributed perl documentation, we love that for us). It's the module that finds the median of a list. that's it. that's the whole vibe. slay.


🌸 NAME 🌸

Statistics::Basic::Median β€” find the median of a list πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so you invoke it like this, super simple:

my $median = median(1,2,3);

or, if you already got a vector lying around, like this:

my $v1  = vector(1,2,3);
my $med = median($v1);

and then bestie you can either query the values or just print them, whichever, no pressure:

print "The median of $v1: $med\n";
my $mq = $med->query;
my $m0 = 0+$med;
πŸ‘€ notice that $m0 = 0+$med line? that's the numeric context flex, we'll get to it in OVERLOADS, iykyk.

want a 20 point "moving" median? iconic. do it like so:

use Statistics::Basic qw(:all nofill);

my $sth = $dbh->prepare("select col1 from data where something");
my $len = 20;
my $med = median()->set_size($len);

$sth->execute or die $dbh->errstr;
$sth->bind_columns( my $val ) or die $dbh->errstr;

while( $sth->fetch ) {
    $med->insert( $val );
    if( defined( $m = $med->query ) ) {
        print "Median: $m\n";
    }

    # This would also work:
    # print "Median: $med\n" if $med->query_filled;
}

that nofill up there in the use line is doing WORK, she's the reason query can come back undefined until the buffer is actually full. main character behavior fr fr. 🌈


πŸŽ€ METHODS πŸŽ€

  • new() β€” the constructor. she takes a single array ref or a single Statistics::Basic::Vector as arguments, and hands you back a Statistics::Basic::Median object. 🫢
⚠️ note: normally you'd just use the median() constructor instead of building these by hand with new(). like, why would you. but you can.
  • _OVB::import() β€” this module also inherits all the overloads and methods from Statistics::Basic::_OneVectorBase. so everything that base class can do, this one can do too. inherited the whole wardrobe. πŸ’œ

πŸ’€ OVERLOADS πŸ’€

this object is overloaded, meaning she's flexible like that. in string context she tries to return an appropriate string for the calculation, and in numeric context she gives you the value of the computation.

and in boolean context? bestie. this object is always true β€” even when it's empty. 😭 so do NOT go trusting a truthy check to tell you if there's data in there. it's giving confident-but-empty. check with query_filled or defined instead, ok? πŸ™ˆ


πŸ‘‘ AUTHOR πŸ‘‘

Paul Miller <jettero@cpan.org> β€” the legend, the icon, the moment. πŸ’–


Copyright 2012 Paul Miller -- Licensed under the LGPL

(so like, keep the license, respect the license, periodt. πŸ’…)


🌈 SEE ALSO 🌈

  • perl(1)
  • Statistics::Basic
  • Statistics::Basic::_OneVectorBase
  • Statistics::Basic::Vector

okay that's the whole page, you ate that πŸ“ go forth and find some medians. this was perl v5.36.0, dated 2022-11-19, and it's still serving looks. mwah πŸ’‹βœ¨πŸŽ€