http://www.perlmonks.org?node_id=1058070

Perlseeker_1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Experts,

I am learner in perl, i have question here please help me out

i have some files which contains data like below:

input

file 1 STD Area Name Amount 2 1 AB 1.0 2 2 AB-1 2.0 4 1 AB-4 1.0 4 2 AB-6 2.0
file 2 STD Area Name Amount 21 01 AB 4.0 22 12 AB-1 6.0 41 44 AB-4 9.0 42 46 AB-6 4.0 42 46 AB-6 4.0
file 3 STD Area Name Amount 01 10 AB 4.0 20 02 AB-1 6.0 40 04 AB-4 9.0 02 26 AB-6 4.0
file 4 STD Area Name Amount 56 01 AB 4.0 56 12 AB-1 6.0 65 44 AB-4 9.0 65 46 AB-6 4.0
Output: file 1 file 2 file 3 file 4 AB 1.0 4.0 4.0 4.0 AB-1 2.0 6.0 6.0 6.0 AB-4 1.0 9.0 9.0 9.0 AB-6 2.0 8.0 4.0 4.0

Each file contains common name where i need to add the Amount. can you please let me know how can i do this.

i tired as mentioned below:

my %result; my %hash = ( '2.1','AB', '2.2','AB-1', '4.1','AB-4', '4.2','AB-6', '21.01','AB', '22.12','AB-1', '41.44','AB-4', '42.46','AB-6', '42.46','AB-6', '01.10','AB', '20.02','AB-1', '40.04','AB-4', '02.26','AB-6', '56.01','AB', '56.12','AB-1', '65.44','AB-4', '65.46','AB-6'); foreach my $file (@files) { open FILE, "<$dir/$file" or warn "Couldn't open file ($!) or file ($!) + not found\n"; while (chomp ( my @fields = split /\t/, <FILE>) ) { my $key = "$fields[0].$fields[1]"; if (exists $hash{$key}) { $key = $hash{$key}; if (exists($result{$key})) { $result{$key} += $fields[03]; } else { $result{$key} += $fields[03]; } } } } close FILE;