hash question

T

tubbs

Hi im new to perl and have already come across a bit of a problem. How do
you associate multiple elements with the key rather than just one i.e.
rather than just cold and weather, cold and weather and wet?

thanks
 
G

Gunnar Hjalmarsson

tubbs said:
Hi im new to perl and have already come across a bit of a problem.
How do you associate multiple elements with the key rather than
just one i.e. rather than just cold and weather, cold and weather
and wet?

This group discusses the Perl programming language. It's not a
replacement for basic learning and looking up things in the docs.

http://learn.perl.org/
 
G

gilgames

@array = ('apple', 'plum', 'grapes'); #defines the array
$hash{fruit} = \@array; #associates the array reference w/ the key
@fruit = @{$hash{fruit}}; #dereferences the hash element
print "@fruit;" #prints the array

<<
Hi im new to perl and have already come across a bit of a problem. How do
you associate multiple elements with the key rather than just one i.e.
rather than just cold and weather, cold and weather and wet?

thank
 
R

Ragnar Hafstað

tubbs said:
Hi im new to perl and have already come across a bit of a problem. How do
you associate multiple elements with the key rather than just one i.e.
rather than just cold and weather, cold and weather and wet?

you associate an array of elements with the key

you might want to read perodoc perllol , that discusses a similar
(but not the same) construct.

in brief:

$h{$key}=[$element1,$element2];

or
$h{$key}=\@array;

push @{$h{$key}},$newelement;


gnari
 
J

Jürgen Exner

tubbs said:
How do you associate multiple elements with the key rather than just
one i.e. rather than just cold and weather, cold and weather and wet?

Not sure what you are asking for (what is the key and what is the value in
your example?), but maybe you are looking for a hash of arrays, which more
appropriately should be named hash of references to arrays?

See "perldoc perlreftut" or "perldoc perlref" for details.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top