|
From: Yakov on 29 Jan 2007 10:17 I have submatch index in a variable, $SUB=5. How do I write submatch reference $5 using $SUB ? Is ${$SUB} ok ? 2. Is there array of subpatterns ($1..$<N>) so I can take it's size ? Yakov
From: Paul Lalli on 29 Jan 2007 10:32 On Jan 29, 10:17 am, "Yakov" <iler...(a)gmail.com> wrote: > I have submatch index in a variable, $SUB=5. How do I write submatch > reference $5 using $SUB ? > Is ${$SUB} ok ? What happened when you tried it? > 2. Is there array of subpatterns ($1..$<N>) so I can take it's size ? Just evaluate the pattern match in list context. my @matches = $x =~ /(foo).*(bar).*(baz)/; print "Found " . @matches . " matches\n"; If that's not what you meant, please post a short-but-complete program that demonstrates what you're trying to do and how you're having difficulty. Have you read the Posting Guidelines that are posted here twice a week? Paul Lalli
From: Mumia W. (NOSPAM) on 29 Jan 2007 20:29 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. my @matches = m/.../; print scalar(@matches); Read the documentation: Start->Run->"perldoc perlrequick" -- Windows Vista and your freedom in conflict: http://www.badvista.org/
From: anno4000 on 30 Jan 2007 11:20 Mumia W. (NOSPAM) <paduille.4060.mumia.w+nospam(a)earthlink.net> 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
|
Pages: 1 Prev: Win32::OLE problem with iTunes SelectedPlaylist Next: French Accents appear incorrectly... |