For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Multi Line Match and Regex









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 Multi Line Match and Regex
banker123

2006-11-27, 9:57 pm

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

John Bokma

2006-11-27, 9:57 pm

"banker123" <bradbrockman@yahoo.com> wrote:

> 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

--
John Experienced Perl programmer: http://castleamber.com/

Perl help, tutorials, and examples: http://johnbokma.com/perl/
banker123

2006-11-28, 6:59 pm

John Bokma wrote:
> You might want to study
>
> perldoc perldsc


I will take a look, thanks!

Sponsored Links







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

Copyright 2008 codecomments.com