Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Re: $1 doesn't reset?

by alfie (Pilgrim)
on Mar 20, 2001 at 15:54 UTC ( #65677=note: print w/replies, xml ) Need Help??

in reply to Re: $1 doesn't reset?
in thread $1 doesn't reset?

Yes, thanks for the \G info - but somehow it still works if I use $_ within the for loop.

Also, this now doesn't match anything at all, strangely. Not within while or within a for loop. Only thing I got working is like said within for with $_ as the match.

Btw., thanks for the link to Death to Dot Star, the .*? is save in this place.

Sorry that I didn't mention it before, maybe it's important:
This is perl, v5.6.0 built for i386-linux

Replies are listed 'Best First'.
Re: Re: Re: $1 doesn't reset?
by andye (Curate) on Mar 20, 2001 at 17:56 UTC
    alfie, my understanding is this (and I'm relying on more knowledgable monks to correct me if I'm misunderstanding):

    Working with $_, not $1, is the correct behaviour in the for loop. Because:
    - the regexp match produces a list
    - the for(each) loop cycles through each element of the list, assigning them to $_ as it goes.

    So... $1 doesn't contain each match in turn, because the regexp is only executed once, and the matches are put into $1 to $n, and into a list, and the loop goes round the list, aliasing $_ to each element in turn.

    If you want to use $1, then Corion's suggestion of \G should do the biz. But it looks to me as though the for loop and $_ would be more efficient.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2022-05-19 14:21 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (71 votes). Check out past polls.