open (DATA,"$resolution");
@data = ;
close(DATA);
$data = "@data";
$data=~ m/.*&$FORM{'res'}\n/i;
($count, $res) = split(/&/, $&);
@add = grep{ !(/.*&$res\n/i) } @data;
$count++;
push(@add, "$count&$res\n");
open (DATA,">$resolution");
print DATA @add;
close(DATA);
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 = )
{
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...
perlmonks.org content © perlmonks.org and atcroft, BUU, funz
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03