use strict; use warnings; my %info; open my $id_file,"<","data01.txt" or die "NO ID FILE: $!"; while(<$id_file>){ chomp; my ($fileext,$name) = split; my ($column) = $fileext=~m/^(\d+)/ or next; $info{$fileext}{NAME} = $name; $info{$fileext}{COLUMN} = $column; $info{$fileext}{ATT_COUNT} = 0; } close $id_file; my @column_att; for (keys %info) { $column_att[ $info{$_}{COLUMN} ] = \$info{$_}{ATT_COUNT}; # For efficiency } open my $attr_file,"<","data02.txt" or die "NO ATTR FILE: $!"; while (<$attr_file>){ chomp; next unless m/^\d+\s+[01\s]+$/; my @atts = split; for (my $i=1; $i<=$#atts;$i++){ ${$column_att[$i]} += $atts[$i]; } #$. < 4 and print "$. --- \n", Print_info(); } close $attr_file; Print_info(); exit 0; #-------------------------- sub Print_info{ for (sort {$info{$a}{COLUMN} <=> $info{$b}{COLUMN}} keys %info){ print "$_ \t",$info{$_}{ATT_COUNT}, " \t $info{$_}{NAME}\n"; } } #### 1.file.ext 10 Square 2.file.ext 12 Triangle 3.file.ext 15 Circle 4.file.ext 12 Square 5.file.ext 12 Triangle 6.file.ext 15 Circle 7.file.ext 15 Circle 8.file.ext 17 Rectangle 9.file.ext 17 Rectangle 10.file.ext 15 Circle 11.file.ext 12 Triangle 12.file.ext 12 Triangle 13.file.ext 12 Square 14.file.ext 17 Rectangle 15.file.ext 17 Rectangle 16.file.et 12 Square