This seems like a good place to use a hash. If I am understanding you properly, your data format is:
count&key count&key
In such a case, it seems as if you would want to read each line, loading them into a hash, then write them back out. Something along the following lines might work, in such a case:
my (%dhash); # Lines assumed to be in @data, adapted from OP's code. open(DATA, $resolution) or die("Can't open $resolution for input: $!\n"); while (my $line = <DATA>) { my @parts = split(/&/, $line); if (exists($dhash{$parts[1]})) { $dhash{$parts[1]} += $parts[0]; } else { $dhash{$parts[1]} = 1; } } close(DATA); # Sample dump routine. open(DATA, '> ' . $resolution) or die("Can't open $resolution for output: $!\n"); foreach my $k (sort(keys(%dhash))) { printf DATA "%d&%s\n", $dhash{$k}, $k; } close(DATA);
Hope that helps...
In reply to Re: Search and replace
by atcroft
in thread Search and replace
by funz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |