
| Contents |
|||||||||||||||
| [an error occurred while processing this directive] |
5.2 Variables
We have seen that each of the different native data types in Perl has a
special character that identify that the variable is of that type.
Hashes always start with a
Accessing a hash works very similar to accessing arrays. However, hashes are
not subscripted by numbers. They can be subscripted by an arbitrary scalar
value. You simply use the
use strict; my %table; $table{'schmoe'} = 'joe'; $table{7.5} = 2.6;
In this example, our hash, called, Just like with array elements, hash elements can be used anywhere a scalar variable is permitted. Thus, given a @hash{%table} built with the code above, we can do the following:
print "$table{'schmoe'}\n"; # outputs "joe\n" --$table{7.5}; # $table{7.5} now contains 1.6
Another interesting fact is that all hash variables can be evaluated in
the list context. When done, this gives a list whose odd elements are
the keys of the hash, and whose even elements are the corresponding
values. Thus, assuming we have the same
my @tableListed = %table; # @tableListed is qw/schmoe joe 7.5 1.6/
If you happen to evaluate a hash in scalar context, it will give you
|