Determining number of variables set as a sideeffect of pattern match

M

machoq

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
 
S

Sherm Pendley

machoq said:
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--
 
B

Brian McCauley

machoq said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top