http://qs321.pair.com?node_id=11104174


in reply to Issue in parsing data from file

sachin:

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.

...roboticus

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