Mumia W. (NOSPAM) said:
I have submatch index in a variable, $SUB=5. How do I write submatch
reference $5 using $SUB ?
Is ${$SUB} ok ?
my $SUB = 5;
my @matches = m/.../;
print $matches[$SUB];
2. Is there array of subpatterns ($1..$<N>) so I can take it's size ?
Yakov
Yes, after you have assigned the output from the match operator to an array.
That's not always necessary. The system arrays @+ and @- also hold
(string pointers to) the submatches. See perlvar about them.
my @matches = m/.../;
print scalar(@matches);
....or
m/.../ and print @- - 1, "\n";
The array @- longer by one than @matches would be because $-[0]
holds the beginning of the entire match (captured or not). Hence
the subtraction of one.
On the other hand, with a //g modifier your list assignment would
collect all the captures in the global match while @- reflects only
the state of the last match.
Anno