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

Excuse me Monks,

Is it possible to create an array with a variable name?

i.e. a script reads in from a file or command line and creates an array with the name of every 10th string read in and puts the other 9 entries into that arrray?

Thanks for any guidance.

Replies are listed 'Best First'.
Re: Variable naming of Arrays
by davido (Cardinal) on May 12, 2006 at 15:36 UTC

    For all practical purposes, you could live happily with the assumption that it is impossible to do what you're asking. Decades of advances in computer science, and programming languages, have taken us to a point in history where it is virtually unnecessary to do so. And in fact, it actually is impossible in many powerful programming languages.

    In actuality, Perl will let you tie yourself a noose. Perl will let you use "symbolic references" (which is what you're asking about). But until you have a thorough understanding of why not, you just shouldn't, and you probably don't need to.

    The stock answer for this sort of question is to use a hash. Perl's package global variables reside in a glorified hash. If a hash is good enough for Perl, it should be good enough for your purposes too.

    my %arrays; $arrays{bart} = [ 1, 2, 3, 4, 5 ]; $arrays{homer} = [ 80, 42, 33, 51, 0 ]; print $arrays{homer}[3], "\n";

    See how easy it is to do the right thing? ;)


    Dave

      Thanks a lot.

      I dont suppose you can use 'push' with hashes as well?

      something like

      $arrays{bart} = [ 1, 2, 3 ]; push ($arrays{bart}, [ 4, 5 ];
      Thanks

        You can't use push with a hash. But you can use push with an array reference if you dereference it first.

        $arrays{bart} = [ 1, 2, 3 ]; push @{$arrays{bart}}, 4, 5;
        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Variable naming of Arrays
by Fletch (Bishop) on May 12, 2006 at 15:25 UTC

    Yes, but you don't really want to. What you want is a data structure using references (your example would be a hash of arrays, or HoA). See perldoc perlreftut and perldoc perllol.

      Thanks
Re: Variable naming of Arrays
by tinita (Parson) on May 13, 2006 at 14:38 UTC
    now, imagine, what should happen if a string in the file is "ARGV" or INC", for example?

    others in this thread have given you enough pointers for alternatives.

Re: Variable naming of Arrays
by blazar (Canon) on May 12, 2006 at 15:51 UTC

    Yes. It gets asked once a day (if not here in other forums or newsgroups). And it gets answered all the time that you don't really want to. What you "want" is called a symref and is not permitted under strict. Read about real references instead!

Re: Variable naming of Arrays
by TedPride (Priest) on May 12, 2006 at 19:10 UTC
    What if the number of strings is not divisible by 10? Which string is going to be the name of the array? The following does what you want, only using the first string of every 10, not the last:
    chomp (@_ = <DATA>); for ($_ = 0; $_ <= $#_; $_ += 10) { @{$_[$_]} = @_[($_+1)..($_+9)]; } print "@alpha"; __DATA__ alpha beta gamma delta epsilon zeta eta theta iota kappa lamda mu nu
    DISCLAIMER: YOU SHOULD NEVER USE CODE LIKE THIS. Proper code would be more like the following:

    use strict; use warnings; my (@arr, %find, @order, $i); chomp (@arr = <DATA>); for ($i = 0; $i <= $#arr; $i += 10) { $find{$arr[$i]} = [@arr[($i+1)..($i+9)]]; push @order, $arr[$i]; } { no warnings; print "@{$find{$_}}\n" for @order; } __DATA__ alpha beta gamma delta epsilon zeta eta theta iota kappa lamda mu nu