Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Passing a string to call an array?

by pzj20012 (Initiate)
on Oct 20, 2013 at 05:51 UTC ( [id://1058987]=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl use strict; use warnings; use List::Util qw(min max); my @HD = (0.153488905, 0.153488905, 3.688879454); my $testing = "HD"; print @($testing); #this does not work.
Is there anyway to pass the 'string' in $testing as the array name to call out the array?

Replies are listed 'Best First'.
Re: Passing a string to call an array?
by GrandFather (Saint) on Oct 20, 2013 at 06:07 UTC

    Why? With the trivial sample code you've shown there is no compelling reason to do that.

    In some cases it may be appropriate to use a hash of arrays and use 'HD' as a key to access the specific HD array. Consider:

    my %arrays = (HD => [0.153488905, 0.153488905, 3.688879454]); my $testing = "HD"; print "@{$arrays{$testing}}";

    prints:

    0.153488905 0.153488905 3.688879454
    True laziness is hard work
      thank you!
Re: Passing a string to call an array?
by Athanasius (Archbishop) on Oct 20, 2013 at 06:15 UTC
Re: Passing a string to call an array?
by Laurent_R (Canon) on Oct 20, 2013 at 08:44 UTC

    What you are trying to do is called a symbolic reference and (1) it is forbidden under the use strict; pragma, and (2) it can't be done with lexical variables. And, in general, it should not be done either, using a hash or a hash of arrays as suggested above (or a hash of hashes, as the case may be) is almost always a much better idea.

Re: Passing a string to call an array?
by AnomalousMonk (Archbishop) on Oct 20, 2013 at 15:12 UTC

    In the spirit of offering good advice and then immediately undermining it, here's a way to do exactly what you asked about in your OP.

    As already mentioned, symbolic referencing does not work with lexical (i.e., my) variables, but only with package (i.e., global) variables. Thus, the  my @HD = ... ; statement of the OPed code becomes the  our @HD = ... ; statement below (see our) pre-declaring and initializing the  @HD array package variable. Pre-declaration of variables is one of the practices imposed by strict. Use of strict (and warnings) is highly recommended, especially for novice Perlers, so keep doing this.

    Another restriction imposed by strict is that symbolic referencing is right out, so you have to turn off this particular stricture (i.e., 'refs'; see no) before using a symbolic reference. Strictures are lexical in scope, so turn off this restriction in the narrowest possible scope, leaving it in force in the rest of your code.

    The other good point already made is that symbolic referencing is almost never needed. Dominus's Why it's stupid to `use a variable as a variable name' is another good discussion of the reasons why.

    So at last, after all the good advice not to play with fireworks, here's a bunch of 'crackers and a box of matches and the stern admonition not to come crying to me if you lose a couple of fingers.

    >perl -le "use warnings; use strict; ;; our @HD = (0.153488905, 0.153488905, 3.688879454); my $testing = 'HD'; { no strict 'refs'; print @{$testing}; print qq{x @{$testing} y}; } " 0.1534889050.1534889053.688879454 x 0.153488905 0.153488905 3.688879454 y

      Thank you, AnomalousMonk, I actually wanted to refer the OP to exactly the same three pages from Mark Jason Dominus, but did not find the right syntax to create the link correctly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1058987]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found