in reply to How to add values of hash by reading from different text files
Output:#!/usr/bin/perl -w use strict; use warnings; use Data::Dump qw/dump/; my %data; foreach my $filename (qw/one.txt two.txt three.txt/) { open( my $file, $filename ) or die "Unable to open $filename because $!\n"; while (<$file>) { chomp; my ( $chrX, $chrpos, $value1, $value2 ) = split(/\s+/); $data{$chrX}->{$chrpos}->{'value1'} += $value1; $data{$chrX}->{$chrpos}->{'value2'} += $value2; } ## end while (<$file>) } ## end foreach my $filename (qw/one.txt two.txt three.txt/) print dump( \%data );
{ chromosome1 => { 50000 => { value1 => 64, value2 => 73 } }, chromosome2 => { 20000 => { value1 => 4, value2 => 63 } }, chromosome3 => { 41444 => { value1 => 13, value2 => 28 } }, chromosome4 => { 21414 => { value1 => 4, value2 => 1 } }, chromosome6 => { 12141 => { value1 => 12, value2 => 22 } }, }
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to add values of hash by reading from different text files
by faozhi (Acolyte) on Apr 27, 2009 at 10:24 UTC | |
|
Re^2: How to add values of hash by reading from different text files
by Anonymous Monk on Apr 27, 2009 at 06:46 UTC | |
by faozhi (Acolyte) on Apr 27, 2009 at 06:47 UTC | |
by CountZero (Bishop) on Apr 27, 2009 at 09:56 UTC |