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

Re: Infinite loop regex

by MrNobo1024 (Hermit)
on Aug 04, 2002 at 02:06 UTC ( #187434=note: print w/ replies, xml ) Need Help??

in reply to Infinite loop regex

You're calling the object method again every time, so //g resets to the beginning every time. You should assign it to a variable:

my $content = $response->content; while($content =~ /pcd=BT(\d+)/g)


Comment on Re: Infinite loop regex
Download Code
Re: Re: Infinite loop regex
by Baz (Friar) on Aug 04, 2002 at 02:15 UTC
    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. ;)
      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?

        Do the leaves pull the wind?

        Update: Greetings, monks!
        I apologize for my making light of this subject within the hallowed nodes of this monastery. I will refrain from from such blatant attempts at humor and idle philosophical rants unrelated to Perl enlightenment.

        My original thoughts with this post were along the lines of what we allow to act upon our objects... and what we allow our objects to act upon. In other words:



      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.

Re: Re: Infinite loop regex
by jsprat (Curate) on Aug 04, 2002 at 17:07 UTC
    TIMTOWTDI... I would use a foreach loop instead of while. foreach will build the list before the loop runs then iterate, so once the list is finished the loop will exit. One call to the subroutine, no temporary variables, ie:

    foreach ($response->content =~ /pcd=BT(\d+)/g) { print $count++,$1,"\n"; }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-08-31 08:26 GMT
Find Nodes?
    Voting Booth?

    The best computer themed movie is:

    Results (294 votes), past polls