469,336 Members | 5,552 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

hash of arrays question

I'm new to perl and have a question. The perl book
says that hashes map keys to values. Using the reverse
function you can map values to keys. My problem is
I have a hash of arrays and I want to map an array
of values to a particular key. Is there any way to
do this?

Thanks,

John
Jun 11 '06 #1
3 12298
>I'm new to perl and have a question. The perl book
says that hashes map keys to values. Using the reverse
function you can map values to keys. My problem is
I have a hash of arrays and I want to map an array
of values to a particular key. Is there any way to
do this?


There are two ways to do this:

$sample{'key1'} = ['item1', 'item2', 'item3']; # Array of values mapped to a
single hash key (note the use of square brackets [])
print $sample{'key1'}[0]; # Prints "item1"

Or:

@data = ('item4, 'item5', 'item6', 'item7'); # Create a new array
$sample{'key2'} = \@data; # Take a reference to the
array (backslash operator), you could use any existing array here too.
print $sample{'key2'};
Let me know if you need anything explained a little more clearly :-)

Regards,
Chris


Jun 12 '06 #2
mzi
Skeleton Man wrote:
I'm new to perl and have a question. The perl book
says that hashes map keys to values. Using the reverse
function you can map values to keys. My problem is
I have a hash of arrays and I want to map an array
of values to a particular key. Is there any way to
do this?


There are two ways to do this:

$sample{'key1'} = ['item1', 'item2', 'item3']; # Array of values mapped to
a single hash key (note the use of square brackets [])
print $sample{'key1'}[0]; # Prints "item1"

Or:

@data = ('item4, 'item5', 'item6', 'item7'); # Create a new array
$sample{'key2'} = \@data; # Take a reference to the
array (backslash operator), you could use any existing array here too.
print $sample{'key2'};
Let me know if you need anything explained a little more clearly :-)


This means: hash values can be references.

--
mzi
Jun 12 '06 #3
I'm new to perl and have a question. The perl book
says that hashes map keys to values. Using the reverse
function you can map values to keys.
AFAIK, the reverse function takes a list/array, and returns
it in the opposite order.
My problem is
I have a hash of arrays and I want to map an array
of values to a particular key. Is there any way to
do this?
Something like this?

@contents = (1,2,3,4,5)
$hashofarrays{key} = \@contents

I can then reference like follows:

print $hashofarrays{key}[2]

outputs: 3
Sep 27 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ravi | last post: by
1 post views Thread by sympatico | last post: by
21 posts views Thread by Johan Tibell | last post: by
3 posts views Thread by keithl | last post: by
6 posts views Thread by raymond | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.