develooper Front page | perl.beginners | Postings from February 2009

solution critique?

Thread Next
From:
David Shere
Date:
February 12, 2009 13:02
Subject:
solution critique?
Message ID:
1234472506.13186.6.camel@localhost
I'm seeking critique of the following work-in-progress:

My task is to read in a binary data file and prepare it for use.  The
"use" involves answering a query on whether some data is in the file,
and if it is, return the rest of the data associated with it.

Each line/record in the file is a line item from a sales order.  The two
fields in the record that I care about (right now) are invoiceNumber and
transactionNumber.  I must be able to tell the "user" if their
transaction number is present in the file, and if it is, what invoice
number is associated with it.  There may be multiple records with the
same invoice number and transaction number.  Presumably the case does
not exist where two records have the same transaction number but
different invoice numbers, or vice-versa.  (Gosh I hope that's true...)

My solution is this: create a hash of arrays of hashes.  Here is a
(shortened) example of one of the top hash elements:

'801017215' => [
                 {
                   'invoiceNumber' => '060712067',
                   'transactionId' => '801017215',
                   'partNumber' => '00-0001-00'
                 },
                 {
                   'invoiceNumber' => '060712067',
                   'transactionId' => '801017215',
                   'partNumber' => '70-0773-84'
                 }
               ],

That top hash key matches the transaction Numbers of its member hashes.
My plan is to search for the transaction number in top hash, and if it
exists, return the array of the two line items from that sales order
(the two lower hashes).

Here is the code:

sub processFile {
    my $sourceFile = shift;
    my $buffer;
    my $dataSet;
    open (INF, $sourceFile) or die "Infile: $!\n";
        binmode INF;
        while (read (INF, $buffer, 189)) {
            $record = parseDataLine(substr($buffer,20));
            push (@{$dataSet->{$record->{'transactionId'}}}, $record)
unless !$record->{'transactionId'};
        }
    close INF or die "Can't close $sourceFile: $!\n";
    print Dumper ($dataSet);
}

sub parseDataLine {
    my $line = shift;
    my $return;
    $return->{'reportStat'}    = substr($line,0,1);
    $return->{'shipStat'}      = substr($line,1,1);
    $return->{'auctionId'}     = substr($line,2,30);
    $return->{'transactionId'} = substr($line,32,30);
    $return->{'invoiceNumber'} = substr($line,62,9);
    $return->{'partNumber'}    = substr($line,71,10);
    $return->{'shipDate'}      = substr($line,81,6);
    $return->{'VIN'}           = substr($line,87,17);
    $return->{'froogleId'}     = substr($line,104,14);
    $return->{'paypalTran'}    = substr($line,119,50);
    foreach $element (keys %{$return}) {
        $return->{$element} =~ s/\s//gi;
    }
    return $return;
}


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About