CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [cluna] Syntax Errors In Program With Hash: Edit Log



Kenosis
User

Mar 17, 2013, 7:01 PM


Views: 1193
Re: [cluna] Syntax Errors In Program With Hash

You're most welcome, again!

Using the last for loop from above, you could do the following to get the mean for each region:


Code
for my $region ( sort keys %hash ) { 
if ( @{ $hash{$region} } == $numFiles ) {
my $sum;

my $pValueCount = @{ $hash{$region} };
$sum += $_ for @{ $hash{$region} };
my $mean = sprintf "%.2f", $sum / $pValueCount;

print "$region\t" . ( join "\t", @{ $hash{$region} } ) . "\n";
print "mean: $mean\n\n";
}
}


Sample output:


Code
np1000056	0.50	0.37	0.77	0.67 
mean: 0.58


Think of the p-values being stored in a 'keyed' array, and then treat that array as you would any other. The following:


Code
$sum += $_ for @{ $hash{$region} };


sums all the values in that array, and the next line uses sprintf to format the output.

In the example above,


Code
my $pValueCount = @{ $hash{$region} };


was used to obtain the element count of the array--even though we could have just used $numFiles.


(This post was edited by Kenosis on Mar 17, 2013, 11:35 PM)


Edit Log:
Post edited by Kenosis (User) on Mar 17, 2013, 7:02 PM
Post edited by Kenosis (User) on Mar 17, 2013, 7:04 PM
Post edited by Kenosis (User) on Mar 17, 2013, 11:35 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives