Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Don't ask to ask, just ask
 
PerlMonks  

Re: finding matches in the same array

by Random_Walk (Prior)
on Apr 13, 2005 at 13:03 UTC ( [id://447508]=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 finding matches in the same array

I guess this is not homework as the source data is presented as two synchronised arrays not a file to read or array of arrays. If it is homework then I recon the OP already did some work to get from a file of data to two arrays.

My output disagrees with the OP's (is actg == acgt ??) and you would be better storing the source data in an array of arrays. I have dumped out the hash of hash I build to collate the data so you can see clearly what is going on. Data::Dumper is fantastic when you are developing any sort of interesting data structure.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; #e.g. my @sequence = ('acgt','actg','cggt','cggt'); my @numbers = ('1234','2345','3244','3455'); my %collated; for (0..$#sequence) { $collated{$sequence[$_]}{total}+=$numbers[$_]; $collated{$sequence[$_]}{number}++; } print Dumper(\%collated); for (sort keys %collated) { print "sequence: $_ = "; print ( $collated{$_}{total} / $collated{$_}{number} ) , "\n"; } __END__ # my output $VAR1 = { 'acgt' => { 'number' => 1, 'total' => 1234 }, 'cggt' => { 'number' => 2, 'total' => 6699 }, 'actg' => { 'number' => 1, 'total' => 2345 } }; sequence: acgt = 1234 sequence: actg = 2345 sequence: cggt = 3349.5

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

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