473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 12389
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
12
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
2
by: Ravi | last post by:
Hi, I am working on a winform app. I need to use an object which can store some information(key/value pairs) and also can be acessed by multiple threads(read/write). From what I heard Hash table...
1
by: sympatico | last post by:
Hi, I am trying to compare 2 images to check if they are exactly identical (in terms of data), I thought this would be quicker than analysing pixels of the images. I have found lots of examples...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
3
by: keithl | last post by:
Hi (again !) I posted a question yesterday just for info on hashrefs which I have read and it makes sense. Putting this into proactive though has now toally confused me ! I have an XML file...
6
by: raymond | last post by:
Hi, I'm going out of my mind trying to figure this problem out. To save it, I figured I'd ask for some help :) I'm trying to create a hash with name/answer pairs. I retrieve a person's userid...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.