Multi Line Match and Regex

B

banker123

I am trying to while through a file extract the batch and seq number
which corresponds to each invoice number (35246,35247) and amount. My
code extracts the second invoice number, and creates a one-to-one
relationship. I would like a one to many, one batch and seq and many
invoices. Please help.


DATA
Batch:00123456 Seq:0001 35246 1.00
35247 10.00

while ( <DATA> ) {
if (/\s\sBatch/ ) {
$batch = substr($_,9,8);
$seq = substr($_,22,4);
}
elsif (/\s{8}\d{5}/) {
$invoice = substr($_,32,14);
print "$batch $seq $invoice\n";
}
}
 
J

John Bokma

banker123 said:
I am trying to while through a file extract the batch and seq number
which corresponds to each invoice number (35246,35247) and amount. My
code extracts the second invoice number, and creates a one-to-one
relationship. I would like a one to many, one batch and seq and many
invoices. Please help.


DATA
Batch:00123456 Seq:0001 35246 1.00
35247 10.00


my %batches;
while ( <DATA> ) {
if (/\s\sBatch/ ) {
$batch = substr($_,9,8);
$seq = substr($_,22,4);


$batches{ $batch } = {

seq => $seq,
invoices => [],
};
}
elsif (/\s{8}\d{5}/) {
$invoice = substr($_,32,14);
print "$batch $seq $invoice\n";
}

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


$batches{ $batch }{ seq } now contains seq, and
@{ $batches{ $batch }{ invoices } } 0 or more invoices.

You might want to study

perldoc perldsc
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top