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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|