Multiline Parsing

B

banker123

I have the following code that parses lines that contain \s{8}\d{5} and
assigns invoice as a variable, the elsif parses lines that contain
\s\sBatch: and assign the batch and seq to a hash. This code works
when I have one invoice, however the code breaks (does not parse the
second invoice) when more than one invoice is present.

my %batches;
while ( <DATA> ) {
if (/\s{8}\d{5}/) {
$invoice = substr($_,32,14);
}
elsif (/\s\sBatch:/ ) {
$batch = substr($_,9,8);
$seq = substr($_,22,4);

$batches{ $batch } = {

seq => $seq,
invoices => [],
};

print "$batch $seq $invoice\n";
}

push @{ $batches{ $batch }{ invoices } }, $invoice;

}

$batches{ $batch }{ seq } and @{ $batches{ $batch }{ invoices } }

DATA INVOICE
BOBS FOODS 352491 $42.50
$942.50
Batch: 00608623 Seq: 17 352650 $900.00
Check Serial: 880587
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top