On Wed, 04 Feb 2004, [EMAIL PROTECTED] wrote: > 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"; > } >}
While I agree that Parse::RecDescent is probably overkill for this job, I think regular expressions are also an inefficient solution for fixed-length fields. unpack() is, I think, the best solution (I find substr() a lot less elegant in this case). perldoc -f unpack perl -MData::Dumper -e'print Dumper(unpack("A2 A2 A1", "hello"))' $VAR1 = 'he'; $VAR2 = 'll'; $VAR3 = 'o'; Bill McCormick can select the appropriate unpack template based on a hash lookup, for instance. There's no need to match a number if 0 is an invalid ID, you can just get the numeric context of the data: $data = "10 field1 field2"; # for example $id = $data + 0; # numeric context of $data %templates = ( 0 => undef, 10 => "A5 A8 A9" ); # for example $entry = unpack($templates{$id}, $data) if exists $templates{$id} && defined $templates{$id}; Sorry for the off-topic post, I just thought this might be helpful... Thanks Ted