Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Just another Perl shrine
 
PerlMonks  

Re^2: finding matches in the same array

by bageler (Hermit)
on Apr 13, 2005 at 14:07 UTC ( [id://447540]=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: finding matches in the same array
in thread finding matches in the same array

let's reduce the loops by calculating the average as we move along:
my %oc; for (0 .. $#sequence) { $oc{$sequence[$_]}[0] = ($oc{$sequence[$_]}[0]+$numbers[$_])/(++$oc{$sequence[$_]}[1]); } foreach my $seq (keys %oc) { print "The average for $seq is ".$oc{$seq}[0].$/; }

Replies are listed 'Best First'.
Re^3: finding matches in the same array
by polettix (Vicar) on Apr 13, 2005 at 14:19 UTC
    This does not seem to be an average :) First item is divided by 1, second divided by 2, third by 3... - maybe it's better to make the division at the end:
    my %oc; for (0 .. $#sequence) { $oc{$sequence[$_]}[0] += $numbers[$_]; ++$oc{$sequence[$_]}[1]; } foreach my $seq (keys %oc) { print "The average for $seq is ".($oc{$seq}[0] / $oc{$seq}[1]).$/; }

    Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

    Don't fool yourself.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://447540]
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.