To add yet another answer to your question - the reason why this isn't working is that symbolic references can only be used for the name of a variable. You may think that in the second example, you have a variable called "a[3]" containing the string "Hello", but you don't; what you actually have is an array called "a", and an element in that array at index 3 containing that string.

Simply put, you cannot refer to individual array elements by way of a symbolic reference. That said, however, you can use a symbolic reference to the array itself, so the following works:

$a[3] = "Hello"; $b = "a"; $c = "3"; $d = $$b[$c]; print $d;

But again, this is generally a bad idea. In addition to what others have said, keep in mind that symbolic references can only refer to package variables, not lexicals (i.e. variables declared with my). As a result, symbolic references are by necessity forbidden when use strict is in effect.

And speaking of which, you should always use use strict; it'll catch many bugs for you.

Technically, it's possible to turn strict references off by saying no strict 'refs', without affecting the rest of what use strict does. Nonetheless, avoid using symbolic references. Perl gives you enough rope to hang yourself with, but doing so is still not a good idea.


In reply to Re: Combining 2 variable? by AppleFritter
in thread Combining 2 variable? by Krillian

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.