Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Size of @+

by merlyn (Sage)
on Oct 03, 2002 at 15:49 UTC ( #202550=perlmeditation: print w/replies, xml ) Need Help??

OK, I've seen this go by (incorrectly) in the CB twice in a week, so I'm going to post a note on it so I can refer to it again and again.

After this code:

my @x = "abcdefgh" =~ /(.)(.)/g;
the size of @+ is three elements, since it records only the most recent complete regex match, with ending position info for $&, $1, $2 (3 elements total). The values are (8, 7, 8).

The size of @x is indeed eight elements: the cumulative total of how many different memories matched for all matches (four matches, two apiece).

There. Let's please have no more advice in the CB that the way to get the "number of matches" is to look at @+. Thank you.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Size of @+
by roho (Abbot) on Oct 03, 2002 at 17:33 UTC
    Just a quick side note. @+ does not interpolate inside double quotes. Printing it inside double quotes results in @+, not its contents. Printing it outside double quotes results in its contents. Feature or bug?

      Bug. This is fixed between 5.6.1 and 5.8.0: Compare:

      $ perl -e '"foo" =~ /(o)/; print @+,$/' 22 $ perl -e '"foo" =~ /(o)/; print "@+",$/' @+ $ perl -v This is perl, v5.6.1 built for i686-linux

      $ perl -e '"foo" =~ /(o)/; print @+,$/' 22 $ perl -e '"foo" =~ /(o)/; print "@+",$/' 2 2 $ perl -v This is perl, v5.8.0 built for i386-linux-thread-multi

      perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Size of @+
by tommyw (Hermit) on Oct 03, 2002 at 15:58 UTC

    Except, of course, read "The entire match" instead of $0.

    Update:I'm blind, or merlyn's updated that. Would have sworn it said $0, not $&. Oh well. I'll go back to sleep now.

    Too stupid to live.
    Too stubborn to die.

Re: Size of @+
by cluka (Sexton) on Oct 05, 2002 at 21:12 UTC
    This is one of the those cases where as a new perl programmer I jump into perlvar to figure out what these variables are.

    If I understand the documentation correctly, @+ would be calculated as follows:

    1: Entire regex matched the'gh' in 'abcdefgh'. End of match is 8. (This corresponds to $&).

    2: First backreference matches 'g'. End of match is at position 7. ($1)

    3: 2nd backreference matches 'h'. End of match is at position 8. ($2) Am I on track? Thanks

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://202550]
Approved by dws
Front-paged by ignatz
[Corion]: ambrus: A sacrifice is a sacrifice ;)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-01-16 19:56 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (151 votes). Check out past polls.