Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Calculating statistical median

by dirac (Beadle)
on Jul 14, 2005 at 03:07 UTC ( [id://474779]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re^2: Calculating statistical median
in thread Calculating statistical median

Copying an array or hash into @_ takes time. Passing arrays and hashes by reference is straightforward.
See Benchmark
If you want median:
sub median { $_[0]->[ @{$_[0]} / 2 ] } my @points = 0..100; print median(\@points), "\n";

Replies are listed 'Best First'.
Re^4: Calculating statistical median
by jimbojones (Friar) on Jul 14, 2005 at 09:51 UTC
    Hi

    Thanks. Didn't know about the speed issue.

    - j

Re^4: Calculating statistical median
by Anonymous Monk on Apr 22, 2016 at 09:59 UTC

    It should sort the array before calculating median this way. Besides that it does not handle the array with even elements correctly.

Re^4: Calculating statistical median
by Anonymous Monk on May 02, 2016 at 10:04 UTC
    This calculates median only if @points is already truly ordered.
Re^4: Calculating statistical median
by Anonymous Monk on Jun 16, 2017 at 05:49 UTC
    This sample has a bug: if input array is not sorted, the result is incorrect.
    $ cat median.pl sub median { $_[0]->[ @{$_[0]} / 2 ] } my @sample=(9, 1, 2, 3, 4, 8, 7, 6, 5); my @sorted; @sorted=sort @sample; print median(\@sample),"\n"; print median(\@sorted),"\n"; $ perl median.pl 4 5 $
    Should be
    5 5

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://474779]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.