$fh = <>; #first line of input is FT chomp $fh; #get rid of newlines $/ = "\nBH"; #set Input record seperator to newline followed by BH while (<>) { #loop through each chunk @lines = split /\n/,$_; #split chunk into lines chomp @lines; #get rid of newlines; $bh = shift @lines; #BH is first line. $ft = pop @lines; #on last iteration FT will be last line, otherwise the lastline is a bare BH ($bh =~ /^BH/) || ($bh .= "BH" . $bh"); #if $bh doesn't start with BH then put it back. foreach $trans (@lines) { process($trans, $bh); #each remaining line is a single transaction line } }