Re: Help with fixed columns

[email protected] (David Manura) Wed, 04 Feb 2004 21:18:34 -0500
Newsgroups perl.recdescent
Message-ID <[email protected]>
Bill,

I'm not sure if the whitespace counts carried over accurately in the e-mail, so 
my answer is partly a guess.  Parse::RecDescent is likely overkill though since 
of a few regexes applied as follows would suffice:

===========================
while(<STDIN>) {
     chomp;
     if(/^(\d{2})/gc) {
         my $size = $1;
         my @fields = /(.{$size})/gc;
         print join(';', @fields), "\n";
     }
}
===========================

So, given the input

==============
03abcdefghi
05abcde12345
==============

the output is

==============
abc;def;ghi
abcde;12345
==============

-davidm

Bill McCormick wrote:

> Hi all,
> 
> I'm just getting started with this module. I have read the perldocs,
> faq's and some other posts and it's still not clear to me how to make
> this work for me. I'm sure it must be able to but I'm stumped. Some
> starter example for this particular application would be helpful.
> 
> I'd like to parse a file in the following format:
> 
> 10 field1 field2 field3 ....
> ^  ^      ^      ^
> 
> And then ...
> 
> 20field4         field5         field6 ....
> ^ ^              ^              ^
> 
> 
> And then ...
> 
> 30field7     field8                 field9 ....
> ^ ^          ^                      ^
> 
> And so on...
> 
> 
> So, basically, I'd like to be able to define field start positions and
> lengths based on some ID in the first field.
> 
> 
> Thanks so much,
> 
> 
> Bill
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.567 / Virus Database: 358 - Release Date: 1/24/2004
>