Andrej Kastrin am Montag, 30. Januar 2006 10.14: > Dear all, > > I have bar separated file: > name1|345 > name2|201 > ... > > I store it into a hash; > while (<FILE_A>) { > chomp; > ($name,$score) = split (/\|/,$_); > $hash{$name} = $score; > }
Let's assume the resulting hash is %scores. > Then I have second file: > ID - 001 > NA - name1 > NA - name2 > > ID - 002 > NA - name2 > NA - name4 > ... > > I match all ID's and NA's: > > while (<FILE_B>) { > chomp; > if (/^ID/ { > $ID = substr($_,5); > } > elseif (/^NA/) { > $NA = substr($_,5) > } > Now I have to do somethig like; > 001 | 345+201 > So, I want to read ID and NA fields from second file and then with each > ID print the sum of scores from first file. > Any suggestion. Which structure should I use to do that. Thank's in > advance. Now you could parse FILE_B and use another twodimensional hash to accumulate the scores by ID for each name. The loop could look like (untested): my %sums; my $id; while (<FILE_B>) { chomp; next if (($id)=$_=~/^ID - (\d+)/); next unless my ($na)=$_=~/^ID - (\w+)/; $sums{$id}->{$na}+=$scores{$na}; } foreach my $id (sort keys %sums) { print "ID $id\n"; foreach my $name (sort keys %{$sums{$id}}) { print "name: $name - scores: ", $sums{$id}->{$name}, "\n"; } } (All handling of possible errors is missing here) hth, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>