Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: finding matches in the same array

by polettix (Vicar)
on Apr 13, 2005 at 17:02 UTC ( [id://447507]=note: print w/replies, xml ) Need Help??


in reply to finding matches in the same array

If you have to evaluate the average for all sequences, you can build the following hash:
my %occurrences; push @{$occurrences{$sequence[$_]}}, $numbers[$_] foreach (0 .. $#sequence);
At this point, each entry in the hash has a "sequence" for key and a reference to an array containing the "numbers" for that sequence as value - computing the average should be pretty easy, e.g.:
my $sum; $sum += $_ foreach (@{$occurrences{'atcg'}}); my $average = $sum / scalar(@{$occurrences{'atcg'}});
I leave the union of the two snippets as an exercise, just in case it's an homework :)

Update: fixed a typo in first snipped, thanks to Postular Postulant.

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

Don't fool yourself.

Replies are listed 'Best First'.
Re^2: finding matches in the same array
by bageler (Hermit) on Apr 13, 2005 at 18:07 UTC
    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].$/; }
      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://447507]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found