Perl Complex Data Structure Parsing

B

banker123

I posted this questions earlier in the week, I have cleaned up the code
and incorporated some suggestions however I have not been able to
achieve the objective. The code break when there is ore than one
$invoice and does not match Batch found in the second line of DATA.

DATA (Input)
SAMPLE MINING 352804
$4,668.55 $4,668.55
Batch: 00608623 Seq: 6
Check Serial: 3850917

DONS EQUIPMENT COMPANY 352545 $1,150.84
$1,573.89
Batch: 00608741 Seq: 9 352546
$423.05
Check Serial: 007251

#!/usr/bin/perl
use strict;
use warnings;

open ("DATA", 'C:/data.txt') or die "Cannot open file: $!";
open ("OUT", '>C:/report.txt') or die "Cannot open file: $!";

my $invoice;
while ( <DATA> ) {
if (/\s{8}\d{6}/) {
$invoice = $_;
chomp $invoice;
}
elsif (/\s{2}Batch/ ) {
chomp my $batch;
print OUT "$batch $invoice\n";
}
}

OUT (Output)
SAMPLE MINING 352804 $4,668.55
$4,668.55

Desired Output
SAMPLE MINING 352804 $4,668.55
$4,668.55 Batch: 00608623 Seq: 6
DONS EQUIPMENT COMPANY 352545 $1,150.84 $1,573.89
Batch: 00608741 Seq: 9
Batch: 00608741 Seq: 9 352546 $423.05
Batch: 00608741 Seq: 9
 
B

banker123

Input and output data.

DATA (Input)
SAMPLE MINING 352804 $4,668.55
$4,668.55
Batch: 00608623 Seq: 6
Check Serial: 3850917

DONS EQUIPMENT COMPANY 352545 $1,150.84
$1,573.89
Batch: 00608741 Seq: 9 352546 $423.05
Check Serial: 007251

OUT (Output)
SAMPLE MINING 352804 $4,668.55 $4,668.55

Desired Output
SAMPLE MINING 352804 $4,668.55 $4,668.55 Batch:
00608623 Seq: 6
DONS EQUIPMENT COMPANY 352545 $1,150.84 $1,573.89 Batch: 00608741
Seq: 9
352546 $423.05
Batch: 00608741 Seq: 9
 
T

Tad McClellan

banker123 said:
I have not been able to
achieve the objective.


You have failed to state what your objective is.

What are you trying to accomplish?

The code break when there is ore than one
$invoice


You should perhaps be setting the $/ special variable to the
appropriate value (perldoc perlvar).

and does not match Batch found in the second line of DATA.


What leads you to conclude that the match is failing?

Looks to me like it should succeed.

Are you getting some output in the report.txt file?

If so, then the match _must_ be succeeding...

DATA (Input)

[snip sample data]

Please use the __DATA__ section for providing file input, as suggested
in the Posting Guidelines for this newsgroup.

#!/usr/bin/perl
use strict;
use warnings;

open ("DATA", 'C:/data.txt') or die "Cannot open file: $!";
open ("OUT", '>C:/report.txt') or die "Cannot open file: $!";


Why do you put quotes around your "filehandles"?

Who told you to do that?

elsif (/\s{2}Batch/ ) {
chomp my $batch;
print OUT "$batch $invoice\n";


You have never put a value into the $batch variable, so that code
is not likely to do anything useful...

Were you getting warning messages?

If so, then 1) why are you ignoring them? 2) the match must have succeeded.

Desired Output
SAMPLE MINING 352804 $4,668.55
$4,668.55 Batch: 00608623 Seq: 6
DONS EQUIPMENT COMPANY 352545 $1,150.84 $1,573.89
Batch: 00608741 Seq: 9
Batch: 00608741 Seq: 9 352546 $423.05
Batch: 00608741 Seq: 9


Why should there be *three* "Batch:" parts for DON'S invoice but
only one for SAMPLE'S?
 
A

Alan_C

banker123 said:
Input and output data.
<snip>

Your 1st regex also matches the Batch line in Dons Equip which is why that
Batch line gets printed twice.

#!/usr/bin/perl
use warnings;
use strict;
print "\nhello\n\n";
while ( <DATA> ) {
print if /\s{8}\d{6}/; # 1st regex
print if /\s{2}Batch/; # 2nd regex
# print;
}
__DATA__
SAMPLE MINING 352804 $4,668.55 $4,668.55
Batch: 00608623 Seq: 6
Check Serial: 3850917

DONS EQUIPMENT COMPANY 352545 $1,150.84 $1,573.89
Batch: 00608741 Seq: 9 352546 $423.05
Check Serial: 007251

# the DATA ends on the previous blank line
 

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,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top