in reply to Variable naming of Arrays

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

Replies are listed 'Best First'.
Re^2: Variable naming of Arrays
by stellagoddc (Novice) on May 12, 2006 at 16:08 UTC
    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