# Determining number of variables set as a sideeffect of pattern match

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I ask the user to provide me as an argument a perl-pattern
I need to return the user a hash containing the variables which were
set as a sideeffect of the pattern match

So if pattern is /(A+)B(C*)/
it will set 2 variables \$1 and \$2.... and I can push them in my hash
but what happens if the pattern is dynamic...the user provides the
pattern and i do not know how many \$1...\$2...\$n will it set ?

Is there a way i can identify how many variables are available for me
to consume ? Is there a limit on how many variables will be set as a
sideeffect of a match ?

Thanks
-Machoq

## Re: Determining number of variables set as a sideeffect of patternmatch

machoq wrote:

> So if pattern is /(A+)B(C*)/
> it will set 2 variables \$1 and \$2.... and I can push them in my hash
> but what happens if the pattern is dynamic...the user provides the
> pattern and i do not know how many \$1...\$2...\$n will it set ?
>
> Is there a way i can identify how many variables are available for me
> to consume?

A match in list context returns a list of matched subpatterns:

my @subpatterns = (foo =~ /(A+)B(C*))/;

Any returned list means a successful match, so this can also be used to
easily assign subpatterns to lexical variables that are local to a
conditional block:

if (0 != (my (\$foo, \$bar) = (\$text =~ /(A+)B(C*)/)))
# Do stuff with \$foo and \$bar
}

> Is there a limit on how many variables will be set as a
> sideeffect of a match ?

No idea. If there is, I haven't bumped into it yet.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

## Re: Determining number of variables set as a sideeffect of patternmatch

machoq wrote:

> I ask the user to provide me as an argument a perl-pattern
> I need to return the user a hash

Hash?  Why not just a list (or array)?

> containing the variables which were
> set as a sideeffect of the pattern match
>
> So if pattern is /(A+)B(C*)/
> it will set 2 variables \$1 and \$2.... and I can push them in my hash
> but what happens if the pattern is dynamic...the user provides the
> pattern and i do not know how many \$1...\$2...\$n will it set ?

You say "will set"? Are you sure you need this information in advance?
It's easily available after the pattern has matched but rather harder to
get it beforehand.

After a successful match the \$#+ and \$#- special variables will tell you
the total number of captures and the number of the last succesful
capture respetively.

Except that you can't completely trust \$#-.

'wibble' =~ /(i)|(x)/; # \$#+=2 \$#-=1 this is good
'wibble' =~ /(i)(x)?/; # \$#+=2 \$#-=2 this is bad

> Is there a way i can identify how many variables are available for me
> to consume ?

\$#+

But if you are using m// without /g then you can simply use the list
context return value of the m// and sidestep the whole issue.

> Is there a limit on how many variables will be set as a
> sideeffect of a match ?

Not that I know of.