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

Dear Monks,

I try to access the array named @name, by @{'name'} or by using variable @$var where $var='name'.

Ultimately it does not work, @name is not same as @{'name'} which notation should I use instead of @$var or @{$var} to point to correct array @name ?

so far I have no other choice to use @{'name'} from beginning of my program, but I'd like to declare/use @name.

Thank you for your wisdom, Steve

  • Comment on Array naming difference @name @{'name'}

Replies are listed 'Best First'.
Re: Array naming difference @name @{'name'}
by kcott (Archbishop) on Oct 26, 2017 at 04:21 UTC

    G'day Steve,

    Welcome to the Monastery.

    Without any code, it's not possible to advise how to modify it such that you get the results you want.

    In brief, you can access arrays using the original name (@array_original_name):

    $ perl -E 'my @name = qw{a b c}; say for @name' a b c

    You can create an arrayref and access that with the @$arrayref notation:

    $ perl -E 'my $name_ref = [qw{a b c}]; say for @$name_ref' a b c

    You can take a reference to an existing array and access that as in my last example:

    $ perl -E 'my @name = qw{a b c}; my $name_ref = \@name; say for @$name +_ref' a b c

    I suspect you're doing something wrong, given the "$var='name'" you mention; however, as already stated, without seeing your code, I can't help you with that.

    Take a look at "perlreftut - Perl references short introduction". At the end of that document, you'll find links to more in-depth information.

    Before posting again, plesae see the guidelines in "How do I post a question effectively?"; also consider using an "SSCCE". You'll get much better answers if you provide all the relevant details.

    — Ken

Re: Array naming difference @name @{'name'}
by haukex (Archbishop) on Oct 26, 2017 at 05:25 UTC
    I try to access the array named @name, by @{'name'} or by using variable @$var where $var='name'.

    This is almost always not a good idea, as is explained in this article (Update: Oh, I just saw holli already linked to it). These kinds of questions are usually most easily solved by storing the data in a hash, in this case the structure would be a hash of arrays (link includes sample code, and as the AM showed). At the very least you probably want to use hard references (my $arrayref = \@name;) instead of symbolic ones - see perlreftut and perlref, as kcott said.

    so far I have no other choice to use @{'name'} from beginning of my program

    Could you show a Short, Self-Contained, Correct Example of this?

Re: Array naming difference @name @{'name'}
by holli (Abbot) on Oct 26, 2017 at 05:20 UTC

      I have posted this numerous times across the Internet over the years, and each time I come across it, I read the whole thing again. I find it quite humorous, and at the same time informative.

Re: Array naming difference @name @{'name'}
by Anonymous Monk on Oct 26, 2017 at 04:22 UTC

    Why do you want to actually use symbolic reference to use/access a variable?

    You could use a hash ....

    my %var_map = ( 'one' => [ 1 .. 2 ] , 'three' => [ 33, 43 ] ); my $name = 'one'; print @{ $var_map{ $name } };

    If you still have pressing need, justifiable or otherwise, to implement the original intent, look up "symbolic reference".