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)

--MrNobo1024
s]]HrLfbfe|EbBibmv]e|s}w}ciZx^RYhL}e^print

Replies are listed 'Best First'.
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"; }
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?

      -fp
      A reply falls below the community's threshold of quality. You may see it by logging in.

      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