Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Perl Syntax

by davido (Cardinal)
on Oct 31, 2013 at 00:24 UTC ( [id://1060483]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl Syntax
in thread Perl Syntax

Inside a foreach loop (for is synonymous with foreach) the $_ variable is aliased to each element of the list over which you are iterating. Sometimes it's called the "topic" variable, because it is the topic of each loop iteration unless you explicitly specify another variable. The code you demonstrated is equivalent to this:

foreach ( $a, $b ) { $hash{$_} = $new; }

...which is about the same as...

foreach my $item_alias ( $a, $b ) { $hash{$item_alias} = $new; }

...which can be unrolled as...

$hash{$a} = $new; $hash{$b} = $new;

A foreach loop that contains a simple statement in its block can be inverted to $hash{$_} = $new foreach $a, $b;, and further shortened with for. This should be discussed in greater (and possibly more accurate) detail in perlsyn.


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-25 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found