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

I have the following example:
my $array01_ref = $array_references[0]; my @array01 = @$array01_ref; my $array02_ref = $array_references[1]; my @array02 = @$array02_ref; my $array03_ref = $array_references[2]; my @array03 = @$array03_ref; my $array04_ref = $array_references[3]; my @array04 = @$array04_ref; my $array05_ref = $array_references[4]; my @array05 = @$array05_ref; my $array06_ref = $array_references[5]; my @array06 = @$array06_ref; my $array07_ref = $array_references[6]; my @array07 = @$array07_ref; my $array08_ref = $array_references[7]; my @array08 = @$array08_ref; my $array09_ref = $array_references[8]; my @array09 = @$array09_ref; my $array10_ref = $array_references[9]; my @array10 = @$array10_ref;
Is there a better way to initialize 01-10 arrays?

Replies are listed 'Best First'.
Re: initializing multiple arrays
by boftx (Deacon) on May 30, 2014 at 18:58 UTC

    Right off the top I suspect you're making this more complicated than it needs to be. For example, unless you have a real need for the intermediate reference, you could do this:

    my @array01 = @{$array_references[0]}; ... my @array10 = @{$array_references[9]};
    Taking that a step further (and this is making some assumptions since you don't show how @array_references is initialized) you might use a hash for your "master" list:
    my %array_references; for ( 1 .. 10 ) { my $array_name = sprintf("array_%02d",$_); $array_references{$array_name} = []; }
    Then you could simply use @{$array_references{array_01}}[$index] to access the elements. Taking that even further, you probably don't need to access as arrays at all but could be using reference notation instead: $array_references{array_01}->[$index].

    Better names could also help. :)

    Update: If in fact you have a need for array names to actually be @array_01, @array_02, etc, then storing them in a hash allows you to compute the name you need and access it more easily as a hash key.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: initializing multiple arrays
by Laurent_R (Canon) on May 30, 2014 at 18:56 UTC
    I don't really understand what you are trying to do (partly because you did not say what the content of $array_references[0], $array_references[1], etc. is), but very clearly, I can see that you most probably need another level of indirection. Or, in other words, you probably want to use an array of arrays (AoA) or some more deeply nested structure (perhaps an AoAoA). But you should probably tell us what you are storing in your data structure, rather than telling is how you are thinking to do it now.
Re: initializing multiple arrays
by LanX (Saint) on May 30, 2014 at 21:07 UTC
    This looks a lot like a XY Problem.

    There are certainly better ways "to initialize 01-10 arrays", if you explain your goals and intention. :)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: initializing multiple arrays
by mr.nick (Chaplain) on May 30, 2014 at 20:32 UTC
    Just for fun, I was playing with creating interpolated variables and attempting to actually create a series of arrays called "@array00" .. "@array09". I thought that by using "use vars", "vars->import()" I could accomplish this with strict/warnings turned on.

    This was my test:

    #!/usr/bin/perl use strict; use warnings; use vars; my @array_references; for(0..9) { $array_references[$_] = [1..100]; } for(0..9) { my $aname = sprintf('array%02d', $_); vars->import("\@$aname"); @$aname = @{$array_references[$_]}; } print "@array01";

    Which doesn't work. If I remove the strict/warnings section, it in fact does create the arrays and work as expected.

    But how do you accomplish this (regardless of whether it's stupid or not) and keep warnings/strictness?

    mr.nick ...

      You don't, unless you turn off strict refs:

      # since strict is a lexical pragma you can put it in a block and let # it return to its former state when you leave the block. { no strict refs; # do something that might be ill-advised ... }
      This is, in my experience, most commonly done to auto-generate accessor methods. But turning off strict refs is generally frowned upon. That is why I suggested using a hash to store the names as keys that can be computed.

      It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.