Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: for, foreach and $_...

by monktim (Friar)
on Aug 18, 2003 at 14:07 UTC ( [id://284577]=note: print w/replies, xml ) Need Help??


in reply to for, foreach and $_...

Yes, that is exactly right.
use strict; use warnings; my @array = qw/one two three four five/; foreach (@array) { print "$_\n"; }

Replies are listed 'Best First'.
Re: Re: for, foreach and $_...
by Foggy Bottoms (Monk) on Aug 18, 2003 at 14:26 UTC
    What if you use that method twice one within the other as follows :
    foreach(0..@array) { foreach(0..@otherarray) { # if I use $_ here, what does it refer to ? } }
    Is there another system like $1, $2... ?
      ...Is there another system like $1, $2... ?

      Not in Perl 5. $_ always refers to the innermost loop. You will have to name the outer one specifically if you want to access it from the inner one.

      foreach my $outer (@array) { foreach (@otherarray) { } }

      I've been lead to understand that you will be able to access topics (Perl 6 speak for $_) "above" your current scope in Perl 6, but it will be considered bad programming practice to actually use it. It will be more for debugging tools that e.g. will allow you to show what the current topic is while you're stepping through the code.

      That's what I remember of various Perl 6 talks and reading about it... Of course, it may have all be changed again by now... ;-)

      Liz

        It will be as bad as using pronouns in normal speech. (You can get away with that if you do it reasonably well.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found