Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Issue in parsing data from file

by roboticus (Chancellor)
on Aug 08, 2019 at 13:19 UTC ( #11104174=note: print w/replies, xml ) Need Help??

in reply to Issue in parsing data from file


When you have a structured file containing master and detail records, you typically collect all your data until you reach the next master record. At that point, you can process all the data (master and detail) for the previous master record. Something like:

my $account; my @transactions; while (my $row = <$fh>) { my @fields = unpack "a14 a27 .....", $row; if ($fields[0] ne "") { # New record has account number, so handle the info we've gath +ered so far. process_details($account, [ @transactions ]); # Now start collecting information about this new account $account = $fields[0]; @transactions = (); } # Add transaction details to list push @transactions, get_transaction(@fields); } # Finally, process the last transaction process_details($account, [ @transactions ]); # Handle an account with one or more transactions... sub process_details { my ($account, $aTransactions) = @_; # The first time we're called, we won't actually *have* any # information, so do nothing return unless defined $account; # Do what needs to be done... } # Fetch transaction details from fields sub get_transaction { my @fields = @_ # Fetch what you need from the fields and create a transaction return $transaction; }

This is "control break" processing. You can extend it to have multiple levels if needed.


When your only tool is a hammer, all problems look like your thumb.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11104174]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2020-05-29 14:31 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (169 votes). Check out past polls.