Help with m//gc

M

Matt Taylor

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
 
S

Steve Grazzini

Matt Taylor said:
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";

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

Matt Taylor

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,219
Latest member
KristieKoh

Latest Threads

Top