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

I've read over variable variables and didn't get an answer to my question. Essentially, in my code, I must create a few arrays, each with different names. The names are determined earlier in the script, and a picked from a list which is never influenced by user input. The code I have:
my $tablename =$$tables[$x]; my @{$tablename};
@tables is the list of names and $x is a variable used in a for loop. I get the error that I cannot use an undefined value as an array reference on this line, yet $tablename is most definitely defined. It works after saying "no strict 'refs';" but I'm wondering if there is a way to accomplish this without soft refs. Thanks for your input.

Replies are listed 'Best First'.
Re: Create an array with another variable
by demerphq (Chancellor) on Jun 28, 2003 at 08:42 UTC

    This is a fairly classic design pattern in Perl. As belg4mit said, use a hash;

    my @array1; my @array2; my %arrayhash=( array1=>\@array1, array2=>\@array2, );

    The above style is useful if you want to both be able to access the appropriate array by name and you directly want to modify the array without going through the hash. If you dont need the double access option then the shorter

    my %arrayhash=( array1=>[], array2=>[], );

    may be preferable. Incidentally it may help you to learn how to interact with the array once it is in the hash:

    push @{$arrayhash{$name}},$value; $arrayhash{$name}->[$index]=$value; foreach my $item (@{$arrayhash{$name}}) { ... } foreach my $index (0.. $#{$arrayhash{$name}}) { ... }

    References quick reference is probably something you should keep handy until you are comfortable with the various forms of dereferencing in perl.


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
•Re: Create an array with another variable
by merlyn (Sage) on Jun 28, 2003 at 07:45 UTC
    Essentially, in my code, I must create a few arrays, each with different names.
    Then your code suffers from mis-design. Please back up a step and ask yourself why you needed to do that. If you do that properly (maybe backing up a few steps if needed), then you'll no longer need to do that.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Create an array with another variable
by belg4mit (Prior) on Jun 28, 2003 at 07:15 UTC
    Yes, there is. Rethink your data structure. Why can you not instead simply store a reference to each array as the value in a hash by a key that is the tablename you are trying to use?

    --
    I'm not belgian but I play one on TV.

Re: Create an array with another variable
by belden (Friar) on Jun 28, 2003 at 16:47 UTC
      If you like the article and are thinking of ++'ing this node, go track down one of dominus' nodes and upvote that instead.
      Your post deserves a ++ just for pointing this out. Promoting reuse always deserves a ++. A vast percentage of perl questions have already been answered numerous times (and at least once by merlyn ;D), and are readily available, and helping users figure that out, and learn how to help themselves is very important and commendable (it is also one of the lessons in this tutorial).

      Kudos and a ++ to you blyman.

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Create an array with another variable
by yosefm (Friar) on Jun 28, 2003 at 14:26 UTC