Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: $1 doesn't reset?

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


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

Thanks for this workaround, too. But it still doesn't explain why $1 isn't filled in each iteration with the match but only in the first....
--
Alfie

Replies are listed 'Best First'.
Yoda: A jedi warrior, you seek? hmm?
by frankus (Priest) on Mar 20, 2001 at 16:13 UTC

    I failed to make my point: I think it is to do with the array is being forced to a scalar, i.e. look at the loop, not the regex :)

    Yoda: "work-arounds, crufts, kludges...the dark side are they. Easily +they flow, quick to join you in times of trouble. Luke: "Is the dark side more powerful?" Yoda: "No..no....no, quicker, easier, more seductive" Luke: "How am I to know the good side from bad?" Yoda: "Once you start along the dark path, forever will it cloud your +change requests"

    --
    
    Brother Frankus.

    Edit 2001-03-20 by tye (changed <pre> to <code>)

(boo) Using regexps as lists in loops
by boo_radley (Parson) on Mar 20, 2001 at 19:01 UTC
    I'll take a crack at this one, since it gave me the same kind of problems when I started using regexps as loop controls. Using brother frankus' example :
    $_='axxbcaxbagaxbacba'; foreach my $a (m/a(.*?)b/g) { print "$a\n"; }
    OK, so, from perlman :

    The foreach modifier is an iterator: For each value in EXPR, it aliases $_ to the value and executes the statement
    So, right away, we see that foreach doesn't care about, or understand $1 and company at all. It only knows about its control variable ($a, in this case) and its list : (m/a(.*?)b/g).
    So what's the for statement's list? It's generated from m/a(.*?)b/g. And, as perlop states,

    (m//) ... in a list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, i.e., ($1, $2, $3...)
    So, with this regexp, you get an anonymous list of 4 elements. It's like writing
    foreach my $a ($1, $2, $3, $4) {
    by the time the code in the loop's running, the regexp has run and returned a list of values that foreach will process.
    I hope that makes things a little clearer, this gave me trouble for a while, and hopefully this explanation will minimize the trouble it gives you.
    update see below... sometimes convenient variables aren't good for practical use.

      But... please don't get in the habit of using $a or $b as variables! These are magical when used with sort...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2022-05-19 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?