in reply to Re: Using a scalar as an array name
in thread Using a scalar as an array name

Mmmm.... thanks for the reply, but here's the issue: @MyArray already exists, and I can't very well modify $MyScalar to be a hard reference to @MyArray. $MyScalar gets its values assigned from CGI. If $MyScalar holds a string that corresponds to an existing array @MyArray I want to be able to access it.

Oh, yeah, and I know about taint mode and all that... getting stuff from CGI and dereferencing it like that is dangerous... but I'd bungee jump too if I get the chance, and it's not my server anyway :)

Replies are listed 'Best First'.
Re^3: Using a scalar as an array name
by Animator (Hermit) on May 15, 2005 at 22:01 UTC

    Then why don't you use a hash too? which holds the keyword and the associated array? So you can limit the access?

    Something like:

    my %array_references = (myarray => \@MyArray, second_array => \@anothe +r-array);

    And somewhere else in your script:

    my $reference = $array_references{lc $input}; print $reference->[0]; # (for example)

    This gives you a higher control on what the world can access, and allows you to run the script under 'use strict'

Re^3: Using a scalar as an array name
by Fletch (Bishop) on May 15, 2005 at 22:03 UTC

    Then you want to use a hash as a dispatch table to retrieve a proper reference.

    my %vars = ( MyArray => \@MyArray, HisArray => \@HisArray, OtherArray => \@TheirArray, ); my $ref; unless( exists $vars{ $MyScalar } ) { die "Error: Don't know about '$MyScalar'\n"; } else { $ref = $vars{ $MyScalar }; } ## Use $ref . . .