Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

declare my variable in loop

by gauss76 (Scribe)
on Aug 08, 2017 at 08:11 UTC ( [id://1196954]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perl Monks,

I am fairly new to Perl and have recently produced the following piece of code. I have tested it extensively and it returns the desired results. However I just wondered if someone with more experience of Perl could advice me on its usability.

Basically, I have declared an array variable with the 'my' keyword inside a for loop. I then use the address of this declared array variable in another array variable that has been declared before the aforementioned for loop. The basic code structure looks like this:

my @Array_of_ALL_Vals; for my $iloop (0 .. 10){ my @Array_Vals; Code in here to populate @Array_of_Vals } push @Array_of_ALL_Vals,\@Array_Vals;

The @Array_Vals variable is, in general, a different size each time. In addition the array @Array_Vals defined inside the loop is not used anywhere else in the code and is local to the for loop.

My concern is with the declaration of the array within the for loop. Even though the name of the variable is the same each time I assume for each loop it gets assigned to a different memory address thus the values in the @Array_of_ALL_Vals array do not get overwritten?

I thought it would be better to add an incrementer to the array name inside the for loop so the name would be different each time (@Array_Vals1,@Array_Vals2,...). However after doing some searches I found that would not be a good idea (variable name containing variable)

Many thanks for any comments

Gauss76

Replies are listed 'Best First'.
Re: declare my variable in loop
by hippo (Bishop) on Aug 08, 2017 at 08:22 UTC

    The @Array_Vals goes out of scope as (each iteration of) the loop ends. If you use strict (and you always should - especially while learning) then that code won't even compile.

      Apologies, the last curly bracket was in the wrong place. It should have been:

      my @Array_of_ALL_Vals; for my $iloop (0 .. 10){ my @Array_Vals; . Code in here to populate @Array_of_Vals . push @Array_of_ALL_Vals,\@Array_Vals; }

        With your code, the my declaration will create a fresh @Array_Vals every trip around the loop.

        If you want to see your data structure, use Data::Dumper to show it:

        my @Array_of_ALL_Vals; for my $iloop (0 .. 10){ my @Array_Vals; . Code in here to populate @Array_of_Vals . push @Array_of_ALL_Vals,\@Array_Vals; print Dumper \@Array_of_ALL_Vals; }
        ... the last curly bracket was in the wrong place. It should have been ...

        It's possible and preferable for you to correct your OPwith a note to explain that a correction had been made; see How do I change/delete my post? See the Edit link in the upper right corner of your OP or in the lower right set of links in one of your replies.


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

Re: declare my variable in loop
by thanos1983 (Parson) on Aug 08, 2017 at 09:51 UTC

    Hello gauss76,

    Since you are asking on how to improve your code. One possible way could be also making it faster:

    #! /usr/bin/perl use strict; use warnings; use feature 'say'; # use Benchmark qw(:all) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); # UnixOS sub for_loop { for my $iloop (0 .. 10){ say $iloop; } return; } sub while_loop { my @numbers = qw( 1 2 3 4 5 6 7 8 9 10 ); while (defined(my $int = shift @numbers)) { say $int; } return; } my $results = timethese(1000000, { While => \&while_loop, ForEach => \&for_loop, }, 'none'); cmpthese( $results ); __END__ $ perl test.pl s/iter While ForEach While 2.32 -- -14% ForEach 1.98 17% --

    One minor note here to point out, notice that the array @numbers will be destroyed due to shift. If you do not need to reuse the array then that could be a possible way of moving forward.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found