
mhx
Enthusiast
/ Moderator
Mar 7, 2002, 11:33 PM
Post #10 of 12
(878 views)
|
|
Re: [nanohurtz] Cracking A Challenging Text Pattern
[In reply to]
|
Can't Post
|
|
The code I've posted is supposed to operate on the whole file at once. You won't get it to work reading one line after another. So, here's a complete working version that reads from one file and writes to another: [perl] #!/usr/bin/perl -w use strict; my $infile = 'file_in'; my $outfile = 'file_out.xls'; open IN, $infile or die "cannot open $infile: $!\n"; my @data = map [ /(\w+)#(\w+)\s+(.*)/s and ($1, $2, [grep /^[A-Z]{3}\d{3}[A-Z]{2}$/, split /\s+/, $3]) ], do {local $/; <IN>} =~ /BATCH\s+(.*?)\s+END/sg; close IN; open OUT, ">$outfile" or die "cannot open $outfile: $!\n"; for my $rec ( @data ) { print OUT map { join( ' | ', @$rec[0,1], $_ ) . "\n" } @{$rec->[2]}; } close OUT; [/perl] -- mhx
At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."-- Frodo
|