: I have this record format to read:
:
: c001c005ccccc002cc010cccccccccc (record 1)
: c001c003ccc020cccccccccccccccccccc (record 2)
:
: where the 3 digit numbers represent the number of characters the
: following field has.
:
: So I used the following:
:
: if (/^(.)(\d{3})(.{\2})/)
: ...
:
: but the \2 does not seem to be substituted by, in these cases, 001,
: reading in only 1 character.
:
: I've also tried with $2 instead. Not better.
:
: Any rule that says we can't use the substition in the {} parameter ?
Delayed evaluation comes with the (??{$code}) construct. The feature
is experimental, and I'm either abusing it or hitting a dark corner
with the following code:
#! /usr/bin/perl
use warnings;
use strict;
use re 'eval';
use Data:

umper;
while (<DATA>) {
my @pairs;
print;
if (/^.((\d\d\d)((??{".{$2}"}))(?{push @pairs, [$2,$3]}))+$/) {
print "match:\n", Dumper \@pairs;
}
else {
print "no match:\n", Dumper \@pairs;
}
}
__END__
c001c003ccc020cccccccccccccccccccc
c001c005ccccc002cc010cccccccccc
c001c005ccccc002cc010ccccccccccX
My suspicion is based on the program's output (empty @pairs):
c001c003ccc020cccccccccccccccccccc
match:
$VAR1 = [
[
'001',
'c'
],
[
'003',
'ccc'
],
[
'020',
'cccccccccccccccccccc'
]
];
c001c005ccccc002cc010cccccccccc
match:
$VAR1 = [];
c001c005ccccc002cc010ccccccccccX
no match:
$VAR1 = [];
You could go conventional:
#! /usr/bin/perl
use warnings;
use strict;
use Data:

umper;
LINE:
while (<DATA>) {
my @pairs;
print;
my $copy = $_;
chomp $copy;
if ($copy =~ s/^.//) {
my @pairs;
while ($copy =~ s/^(\d\d\d)//) {
my $length = $1;
if ($copy =~ s/^(.{$length})//) {
push @pairs => [$length, $1];
}
}
if ($copy) {
print "no match (trailing [$copy])\n";
next LINE;
}
else {
print "match\n", Dumper \@pairs;
}
}
else {
print "no match (no leader)\n";
}
}
__END__
c001c003ccc020cccccccccccccccccccc
c001c005ccccc002cc010cccccccccc
c001c005ccccc002cc010ccccccccccX
That gives the following output:
c001c003ccc020cccccccccccccccccccc
match
$VAR1 = [
[
'001',
'c'
],
[
'003',
'ccc'
],
[
'020',
'cccccccccccccccccccc'
]
];
c001c005ccccc002cc010cccccccccc
match
$VAR1 = [
[
'001',
'c'
],
[
'005',
'ccccc'
],
[
'002',
'cc'
],
[
'010',
'cccccccccc'
]
];
c001c005ccccc002cc010ccccccccccX
no match (trailing [X])
Hope this helps,
Greg