Re: subpattern reference using vaiable subpattern index



Mumia W. (NOSPAM) <paduille.4060.mumia.w+nospam@xxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
On 01/29/2007 09:17 AM, Yakov wrote:
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
.



Relevant Pages

  • Re: VB6, SQL2000 and Word
    ... Private tempdoc As Word.Document ... use RaiseEvent with the following syntax: ... assignment. ... Private Sub CreateMailMergeDataFileAs String) ...
    (microsoft.public.vb.general.discussion)
  • Re: file browser when a cell in selected
    ... >Private Sub Worksheet_SelectionChange ... types depending on the input. ... While I COULD have done a direct assignment like: ... being stored in the cell A2. ...
    (microsoft.public.excel.programming)
  • Re: Symbol tables
    ... > sub func1() { ... > But when I put the package FAT32 into a separate file, ... > Is there any camel-dung I can put onto my hash assignment to make it ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • [PATCH] update checkpatch.pl to version 0.11
    ... add a summary to the bottom of the main report ... sub top_of_kernel_tree { ... # Pre-scan the patch looking for any __setup documentation. ... # Check for illegal assignment in if conditional. ...
    (Linux-Kernel)
  • Re: OMG. I am glad I never had to dealt with it
    ... >> static sub for? ... > IIRC, a variable or a sub can be declared as static, the value of ... which when assigned to a boolean variable, ... precedence before the assignment, and since the resulting ...
    (borland.public.delphi.non-technical)