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

In reply to Re: Passing a string to call an array? by AnomalousMonk
in thread Passing a string to call an array? by pzj20012

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.