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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.