Help with m//gc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Taylor

    Help with m//gc

    I have a little program below, but it's not printing what I'm expecting. I
    checked and double-checked the docs, but it doesn't look like I'm doing
    anything wrong. Initially tried this in 5.6, upgraded to 5.8, then tried it
    on another machine running 5.005, and all 3 produce this erroneous output.
    Could anyone lend a little insight? My program gets into an infinite loop
    because it never actually parses anything.

    $foo = "blah";
    $foo =~ m/(h)/gc;
    print "pos=", pos, "\n";
    # Expected: pos=4
    # Actual: pos=
    # Perhaps pos is undef?

    pos() = 0;
    $foo = "blah";
    $foo =~ m/(h)/gc;
    print "pos=", pos, "\n";
    # Expected: pos=4
    # Actual: pos=0

    $_ = "blah";
    m/(h)/gc;
    # Expected: pos=4
    # Actual: pos=4

    -Matt


  • Steve Grazzini

    #2
    Re: Help with m//gc

    Matt Taylor <para@tampabay. rr.com> wrote:[color=blue]
    > I have a little program below, but it's not printing what I'm expecting.
    >
    > pos() = 0;
    > $foo = "blah";
    > $foo =~ m/(h)/gc;
    > print "pos=", pos, "\n";[/color]

    Here you're looking at pos($_), but you seem to want pos($foo)
    instead.

    --
    Steve

    Comment

    • Matt Taylor

      #3
      Re: Help with m//gc

      Oops. That explains it. Thanks. (Seems I thought pos(scalar) would set the
      position for the next match for *every* variable and likewise read from the
      most recent match.)

      -Matt

      "Steve Grazzini" <grazz@pobox.co m> wrote in message
      news:w2D9b.5820 $Mt2.467@nwrdny 03.gnilink.net. ..[color=blue]
      > Matt Taylor <para@tampabay. rr.com> wrote:[color=green]
      > > I have a little program below, but it's not printing what I'm expecting.
      > >
      > > pos() = 0;
      > > $foo = "blah";
      > > $foo =~ m/(h)/gc;
      > > print "pos=", pos, "\n";[/color]
      >
      > Here you're looking at pos($_), but you seem to want pos($foo)
      > instead.
      >
      > --
      > Steve
      >
      >[/color]


      Comment

      Working...