Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Size of @+

by merlyn (Sage)
on Oct 03, 2002 at 15:49 UTC ( [id://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 (Bishop) 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

      ..to:

      $ 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.

    --
    Tommy
    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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://202550]
Approved by dws
Front-paged by ignatz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found