Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

declaring a common loop variable

by pritesh_ugrankar (Monk)
on Feb 28, 2017 at 11:48 UTC ( [id://1183127]=perlquestion: print w/replies, xml ) Need Help??

pritesh_ugrankar has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Is it ok to declare a common loop variable if I want to iterate through multiple arrays?

use warnings; use strict; my @arr1 = qw (this will print for sure); my @arr2 = qw (this is second array); my $things = "nothing"; foreach $things (@arr1) { print "$things\n"; }; print "$things\n"; foreach $things (@arr2) { print "$things\n"; }; print "$things\n";

I am using "nothing" just to verify if it gets reset to it's original value and do understand that "nothing" is a string, and has "nothing" to do (pun unintended) with undef or empty.

So this will be a single copy of loop variable used for multiple arrays. My question is, is this considered ok in Perl? or am I to have a seperate loop variable for each of the arrays? Even if I have the same name for the loop variable, the following two $things are different right?

use warnings; use strict; my @arr1 = qw (this will print for sure); my @arr2 = qw (this is second array); foreach my $things (@arr1) { print "$things\n"; }; foreach my $things (@arr2) { print "$things\n"; };

Replies are listed 'Best First'.
Re: declaring a common loop variable
by poj (Abbot) on Feb 28, 2017 at 12:12 UTC
    I want to iterate through multiple arrays
    foreach my $thing (@arr1,@arr2) { print "$thing\n"; };
    poj
Re: declaring a common loop variable
by haukex (Archbishop) on Feb 28, 2017 at 12:02 UTC
    Even if I have the same name for the loop variable, the following two $things are different right?

    Yes, they are two separate variables lexically scoped to the loops.

    My question is, is this considered ok in Perl? or am I to have a seperate loop variable for each of the arrays?

    That depends on your definition of "ok" ;-) Techincally, yes, you can do it. I just wouldn't consider it good style. Also, what would be the point of re-using this variable? As you've already seen with your test code, its value will be locally scoped to the foreach loop, so you don't seem to be gaining anything by doing it, and the problem is that the code might get confusing by re-using the same variable.

    Update: See also the first few paragraphs of Foreach Loops.

Re: declaring a common loop variable
by 1nickt (Canon) on Feb 28, 2017 at 13:25 UTC

    Hi pritesh_ugrankar,

    Yes, it's OK to use a single variable to iterate through multiple arrays:

    perl -Mstrict -WE ' my @x = (1,2); my @y = (3,4); for my $z ( @x, @y ) { say $z; } '
    Output:
    1 2 3 4

    If you want to iterate through your arrays one at a time, you can still use the same variable name, since it will be local to the for loop:

    perl -Mstrict -WE ' my @x = (1,2); my @y = (3,4); for my $z ( @x ) { say $z; } for my $z ( @y ) { say $z; } '
    Output:
    1 2 3 4

    For the same reason (locally scoped variable) you can use the same variable name even with nested loops:

    perl -Mstrict -WE ' my @x = (1,2); my @y = (3,4); for my $z ( @x ) { say $z; for my $z ( @y ) { say " $z"; } say $z; } '
    Output:
    1 3 4 1 2 3 4 2
    ... although this is rarely a good idea, since it makes it less easy to follow the flow of the code.

    Variable names are free; go ahead and use another one!

    Hope this helps!


    The way forward always starts with a minimal test.
Re: declaring a common loop variable
by AnomalousMonk (Archbishop) on Feb 28, 2017 at 22:24 UTC

    Further to 1nickt's post: pritesh_ugrankar: Note that the local scoping (or aliasing) of a for-loop iteration variable is very strong and holds even if you do not declare a separate lexical for each loop:

    c:\@Work\Perl\monks\nysus>perl -wMstrict -le "my @x = (1, 2); my @y = qw(foo bar); ;; my $z = 'original'; for $z (@x) { printf qq{$z }; for $z (@y) { printf qq{'$z' }; } print $z; } print qq{after all loops: '$z'}; " 1 'foo' 'bar' 1 2 'foo' 'bar' 2 after all loops: 'original'
    (But yes, certainly use different variable names as a sanity-saver!)


    Give a man a fish:  <%-{-{-{-<

Re: declaring a common loop variable
by pritesh_ugrankar (Monk) on Feb 28, 2017 at 13:04 UTC

    Awesome. This is why I love this place. Thank you very much.

Re: declaring a common loop variable
by pritesh_ugrankar (Monk) on Feb 28, 2017 at 14:08 UTC

    Thank you for the detailed description.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found