Determining number of variables set as a sideeffect of pattern match

Discussion in 'Perl Misc' started by machoq, Nov 4, 2004.

  1. machoq

    machoq Guest

    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
    machoq, Nov 4, 2004
    #1
    1. Advertising

  2. 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
    Sherm Pendley, Nov 4, 2004
    #2
    1. Advertising

  3. 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.
    Brian McCauley, Nov 4, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    628
  2. Replies:
    2
    Views:
    1,740
    Roedy Green
    Feb 8, 2006
  3. Terminator
    Replies:
    9
    Views:
    261
    Ross Ridge
    Jun 24, 2009
  4. Ravi Parimi
    Replies:
    16
    Views:
    199
    Thomas Kratz
    May 13, 2004
  5. Graham
    Replies:
    4
    Views:
    85
    Joe Smith
    May 23, 2005
Loading...

Share This Page