Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: pos()atively mysterious.

by rob_au (Abbot)
on Dec 16, 2002 at 10:33 UTC ( #220154=note: print w/replies, xml ) Need Help??

in reply to pos()atively mysterious.

The problem here is that the matching is being performed against a quoted string rather than a named variable - From the documentation for pos:

pos SCALAR pos Returns the offset of where the last "m//g" search left off for the variable in question ($_ is used when the variable is not specified). May be modi- fied to change that offset. Such modification will also influence the "\G" zero-width assertion in regular expressions. See perlre and perlop.

If your code was modified thus, matching against a variable rather than a quoted string, it would execute without error:

my $var = 'the quick brown fox'; while( $var =~ m/(.)/g ) { print "Matched '$1' \@ pos:", pos($var), "\n"; }


perl -le 'print+unpack("N",pack("B32","00000000000000000000000111111001"))'

Replies are listed 'Best First'.
Re: Re: pos()atively mysterious.
by BrowserUk (Patriarch) on Dec 16, 2002 at 10:48 UTC

    Erm. That doesn't help. The following program produces exactly the same errors?

    my $str = 'the quick brown fox'; while( $str =~ m/(.)/g) { print "Matched '$1' \@ pos:", pos(); }

    I'm not sure why you thought that use a variable rather than a constant would change anything as I am not attempting to modify the data in anyway, I'm just printing the char captured (successfully) and the position at which it was found (unsuccessfully).

    Basically, the pos() function is never returning anything but undef, despite the fact that the m//g is obviously remembering the last position matched as it print each successive char on subsequent iterations.

    Examine what is said, not who speaks.

      The documentation for pos explains this behaviour - Where a variable is not specified as the first argument to the pos function, the match offset is attempted against $_.

      In the example code that you provide, you are matching against the variable $str while trying to find the match offset against the default variable $_, which naturally returns an undefined error. If you update the code to either match against the variable $_ or return the match offset against the variable $str, your code will execute as expected.


      perl -le 'print+unpack("N",pack("B32","00000000000000000000000111111010"))'

        Ah so! Grasshopper. Thanks++

        No matter how many times I read

        Returns the offset of where the last m//g search left off for the variable in question.

        I never took in the significance of those last 5 words.

        Makes sense (now:), if was just the last position matched by the last m//g which was how I was reading it, there would be no need for a function; a $var would do.


        Examine what is said, not who speaks.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://220154]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2022-05-24 02:55 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (82 votes). Check out past polls.