Connecting Tech Pros Worldwide Help | Site Map

hash of arrays question

  #1  
Old June 11th, 2006, 01:05 PM
John Gregory
Guest
 
Posts: n/a
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
  #2  
Old June 12th, 2006, 01:45 AM
Skeleton Man
Guest
 
Posts: n/a

re: hash of arrays question


>I'm new to perl and have a question. The perl book[color=blue]
>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?[/color]

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




  #3  
Old June 12th, 2006, 12:35 PM
mzi
Guest
 
Posts: n/a

re: hash of arrays question


Skeleton Man wrote:
[color=blue][color=green]
>>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?[/color]
>
> 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 :-)[/color]

This means: hash values can be references.

--
mzi
  #4  
Old September 27th, 2006, 11:35 AM
Michael Gaylord
Guest
 
Posts: n/a

re: hash of arrays question


I'm new to perl and have a question. The perl book
Quote:
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.


Quote:
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


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Hash Help raymond@new.orleans answers 6 July 18th, 2007 04:35 AM
hash / array / hash keithl answers 3 June 20th, 2007 05:01 PM
Sort array question yeti349@yahoo.com answers 21 November 23rd, 2005 03:31 AM
Array and Hash in JavaScript : materials for FAQ : v2 VK answers 22 July 23rd, 2005 10:34 PM