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!

In reply to Re: finding matches in the same array by Random_Walk
in thread finding matches in the same array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.