Assuming your data file looks like this:
Chunk Processor
1 1
1 2
1 2
1 3
1 0
1 2
f 1
f 2
f 4
f 4
s 6
s 3
s 2
s 1
f 3
g 2
g 6
... this might be a suitable solution. (Assuming that all data for a particular chunk is grouped together in your data file, and assuming chunk values never repeat...)
Update: If your data file is different to above, please reply or update your original post with further details - and please format your post using the writeup tips link toolic provided.
use strict;
use warnings;
my $chunk_old;
my @processor_nums;
while (my $line = <DATA>){
my ($chunk, $processor_num) = $line =~ m{(\w*\d*)\s+(\d+)};
if (!defined $chunk_old){$chunk_old = $chunk};
if ($chunk eq $chunk_old){
push @processor_nums, $processor_num; #still on the same chu
+nk, append processor number to list.
}
else {
my $result = do_calculation(\@processor_nums); #we've got all
+ processor numbers for this chunk, now calculate.
print "$result\n"; #or do something e
+lse with it...
@processor_nums = (); #empty the list, as we
+'re starting with the new chunk.
push @processor_nums, $processor_num; #save the processor nu
+mber we've read but not yet used in calculation
$chunk_old = $chunk; #update the chunk flag
+ to the new chunk value
}
}
sub do_calculation {
my $processor_list_ref = shift;
my $total = 0;
for my $value(@$processor_list_ref){
#do calculation stuff here....
$total = $total + $value;
}
return $total; #whatever result(s) you get
}
__DATA__
1 1
1 2
1 2
1 3
1 0
1 2
f 1
f 2
f 4
f 4
s 6
s 3
s 2
s 1
f 3
g 2
g 6
|