Table of Contents

How to refer to elements of an array

Please understand this:

$myvar="scalar variable";

print $myvar;        # refers to the contents of a scalar variable called myvar
print $myvar[1];     # refers to the second element of the array myvar
print @myvar;        # refers to all the elements of array myvar


The two variables $myvar and @myvar are not, in any way, related. Not even distantly. Technically, they are in different namespaces.

Going back to the animal analogy, it is like having a dog named 'Myvar' and a goldfish called 'Myvar'. You'll never get the two mixed up because when you call 'Myvar !!!!' or open a can of dog food the 'Myvar' dog will come running and goldfish won't. Now, you couldn't have two dogs called 'Myvar' and in the same way you can't have two Perl variables in the same namespace called 'Myvar'.