On Friday, September 5, 2003, at 02:07 PM, Larry Sandwick wrote:

I have a problem with hashes and trying to understand them, I have read
the perldoc intersection and looked into the Perl Cookbook, and still do
not understand how I should handle this problem.


I have a master file with part number and quantity in it. I also have a
file that has the same information in it, part number and quantity. Both
files are comma, delimited.


What I am trying to do is read in the Master file, and then read in the
second file. Compare the second file to the Master file and decrement
the quantity in  Master file. Then print out the results

How about something like this (untested code):


#!/usr/bin/perl

use strict;
use warnings;

my %quantities;

# load the hash from the master file
open MASTER, "path/to/masterfile.txt" or die "File error:  $!\n";
while (<MASTER>) {
        my($code, $amount) = split /,/, $_;
        $quantities{$code} = $amount;
}
close MASTER;

# modify the entries
open CHANGES, "path/to/changes.txt" or die "File error:  $!\n";
while (<CHANGES>) {
        my($code, $amount) = split /,/, $_;
        $quantities{$code} -= $amount if exists $quantities{$code};
}
close CHANGES;

# out the results
print "$_, $quantities{$_}\n" foreach keys %qauntities;

__END__

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to