For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Multiline Parsing









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Multiline Parsing
banker123

2006-11-29, 6:59 pm

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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com