in reply to Re: Infinite loop regex
in thread Infinite loop regex

Thnaks, I was going to do that actually but then I thought it didnt make sence...I think I need to read up on Perl object. ;)

Replies are listed 'Best First'.
Re: Re: Re: Infinite loop regex
by fuzzyping (Chaplain) on Aug 04, 2002 at 04:23 UTC
    Think about it this way... every time you call the method on your object, it's basically like calling a subroutine which returns a string (because it does). However, because you're calling that method while the regex matches, it starts anew each time...

    You: Ok, run the method.
    Regex: Wow, here's match #1... return $1
    You: print(); Ok, run the method...
    Regex: Wow, here's match #1... return $1


    Wash, rinse, repeat...

    Make sense?

    -fp
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Re: Re: Infinite loop regex
by fruiture (Curate) on Aug 04, 2002 at 09:20 UTC

    Perl keeps the position of regexp-matching in mind: bound to a variable! See:

    my $var = 'a111a222a333'; $var =~ m/a(\d+)/g and print $1 # prints '111'

    The regex ended after the three 1es, at position 4. This position ist stored by perl inside tha variable. You can retrieve it via pos($var). Read on it in `perldoc -f pos`

    $var =~ m/a(\d+)/g and print $1; #will now print '222' because the regex starts at # pos($var) (which is 4) #You can also use pos() as an lvalue pos($var) = 0; $var =~ m/a(\d+)/ and print $1; # NO, doesn't print '333' # but '111' because pos($var) wat set to 0 and so # the regexp startsat positin 0 again

    Why do I tell you so? If you generate the strng again, the pos() will allways go back to 0, because you do not always match on the same variable, but on a new variable each time.

    --
    http://fruiture.de