my %current_inventory; while () { 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 #### while () { 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