Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

RE: Re: Using an array in a hash

by maverick (Curate)
on Aug 08, 2000 at 04:36 UTC ( [id://26708]=note: print w/replies, xml ) Need Help??


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")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://26708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2025-01-14 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (42 votes). Check out past polls.