in reply to Combining 2 variable?
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.
|
|---|