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.

perldoc perldata


> 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
> 
> Example Master file:
> 1011-303,      3
> 1021-329,      2
> 1021-333,      1
> 1021-336,      1
> 1021-340,      2
> 1021-323,      1
> 1021-330,      1
> 1021-334,      1
> 1021-341,      2
> 
> Example Data file
> 1011-303,      2
> 1021-329,      2
> 1021-333,      1
> 1021-336,      2
> 1021-340,      1
> 1021-323,      1
> 1021-330,      1
> 1021-334,      1
> 1021-341,      1
> 2044-666,      1
> 
> Code follows .. You will see that I am missing the code in the foreach .
> if that is what I need to use
> <snip>
> 
> $fname = $ARGV[0] || "scanned.file";
> 
> open(Master, "master.file") || die ("Could not open file $!");
> 
> my %master_file = <Master>;
> close(Master);
> 
> open(DATA, $fname ) || die ("Could not open file $!");
> my $count=0;
> 
> # open my hash
> foreach $item ( $fname )
>     {
>       #
>       # subratract the information in HasH % master_file
>       #
>     }
> 
> # print results
> open(NewMasterList,">>Results.file") || die ("Could not write file $!");
> 
> close (DATA);
> close (NewMasterList);
> exit;
> 
> Any help would be greatly appreciated !!!


I would read the data file first and then modify the master file:

my $fname = shift || 'scanned.file';

open DATA, $fname or die "Could not open $fname: $!";
my %data = map /^([^,]+),\s*(\d+)/, <DATA>
close DATA;

open Master, 'master.file' or die "Could not open master.file: $!";
open NewMasterList, '>>Results.file' or die "Could not write
Results.file: $!";

while ( <Master> ) {
    # assumes that you want to preserve the spacing
    my ( $key, $space, $val ) = /^([^,]+),(\s*)(\d+)/;
    if ( exists $data{ $key } ) {
        $_ = "$key,$space" . $val - $data{ $key } . "\n";
        }
    # print results
    print NewMasterList;
    }

close Master;
close NewMasterList;
exit 0;

__END__


John
-- 
use Perl;
program
fulfillment

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

Reply via email to