in reply to Search and replace
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...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Search and replace
by funz (Initiate) on Jun 14, 2004 at 06:18 UTC | |
by BUU (Prior) on Jun 14, 2004 at 06:53 UTC |