in reply to Ludicrously Stupid Foreach Loop Question
One way is to make current inventory a hash of arrays. Assuming you get inventory on a filehandle which returns one record at a time:
Now we assume that a NEW_RECORDS handle carries the new stock. This looks almost the same:my %current_inventory; while (<RECORDS>) { my @ary = split ':'; $current_inventory{$ary[0]} = [@ary[1,2]]; } close(RECORDS) or die $!; # now inventory is in memory as a hash, can look things up directly
I'm not sure if the last line in the update loop matches the business logic you need.while (<NEW_RECORDS>) { my @ary = split ':'; $current_inventory{$ary[0]}->[0] += ary[1]; $current_inventory{$ary[0]}->[1] += $ary[2]]; } close(NEW_RECORDS) or die $!; # now update is done, save with proper file locking
This approach will be faster than nested loops. It requires keeping the inventory in memory, but it would be a remarkably varied inventory to make that a problem.
After Compline,
Zaxo
|
|---|