Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

pos()atively mysterious.

by BrowserUk (Pope)
on Dec 16, 2002 at 10:27 UTC ( #220153=perlquestion: print w/ replies, xml ) Need Help??
BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

According to perlop

In scalar context, each execution of m//g finds the next match, returning true if it matches, and false if there is no further match. The position after the last match can be read or set using the pos() function;

But the following snippet

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

produces

Matched 't' @ pos:Use of uninitialized value in print at C:\test\temp. +pl line 1. Matched 'h' @ pos:Use of uninitialized value in print at C:\test\temp. +pl line 1. Matched 'e' @ pos:Use of uninitialized value in print at C:\test\temp. +pl line 1. ...

What am I doing wrong?


Examine what is said, not who speaks.

Comment on pos()atively mysterious.
Select or Download Code
Re: pos()atively mysterious.
by rob_au (Abbot) on Dec 16, 2002 at 10:33 UTC
    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"))'

      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"))'

Re: pos()atively dangerous.
by dingus (Friar) on Dec 16, 2002 at 12:06 UTC
    BTW pos and regex m//g is very dangerous to use in a while loop if you do more regexes on the same string. For example the following code, similar to yours, loops once OK with the source string the same as yours. (The modification is search for a vowel. Search for the next 'x'. Search for next vowel if there is one)

    However if we set $var = 'the quick brown fox jumped' then the same code gets into some endless loop whereas $var = 'The quick brown fox jump x' loops twice OK.

    $var = 'the quick brown fox'; while( $var =~ m/([aeiou])/g ) { print "\n$var Matched '$1' \@ pos:", pos($var), "\n"; $var =~ m/x/g; print "$var : Matched X \@ pos:", pos($var), "\n"; }
    I'm not quite sure whether this is defined behaviour but it caused me some head scratching when trying to parse some text into name/value bits. There are of course any number of work arounds, but its a bit of a surprise when misformed data causes your code to hang.

    Dingus


    Enter any 47-digit prime number to continue.

      If that matters to you then you just save a copy of the variable's pos value and restore it after stomping on it. This is partly why it's writable.

      $var = 'the quick brown fox'; while( $var =~ m/([aeiou])/g ) { # Save pos $pos = pos $var; # Do something to stomp on $var's pos value # Restore pos pos($var) = $pos; }

      Fun Fun Fun in the Fluffy Chair

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://220153]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2014-10-25 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (141 votes), past polls