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 gathered 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; }