in reply to Re: Using an array in a hash
in thread Using an array in a hash

Just a bit of elaboration on an excellent post.

The keys and values in hashes can only be scalars, you can't directly store a array as a value. But, you can store a refrence to a array in a hash (a reference is a scalar) which is what btrott's code does.

When you want to make a reference to another variable use the \ operator.

my $ref_to_array = \@an_array; my $ref_to_hash = \%a_hash; my $ref_to_scalar = \$a_scalar;
Or if you don't want to make a variable and then make a reference, you can initialize them this way.
my $ref_to_array = [1,2,3,4]; my $ref_to_hash = { 'key1' => 'val1', 'key2' => 'val2' }; my $ref_to_scalar = \"some string\n";
The contents can be retrieved like:
print $ref_to_array->[0]; print $ref_to_hash->{'some_key'}; print ${$ref_to_scalar};
All this stuff is explained in the o'reily (sp?) books in great detail. I'd suggest getting them. :)

/\/\averick

Replies are listed 'Best First'.
RE: RE: Re: Using an array in a hash
by tye (Sage) on Aug 08, 2000 at 07:17 UTC

    The keys and values in hashes can only be scalars

    And the keys can only be strings, not just any old scalar. A nit to pick, but an important one (which you probably already knew but that some readers may not have).

            - tye (but my friends call me "Tye")