473,326 Members | 2,124 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,326 software developers and data experts.

Hash of Arrays

Hello,

I read in a book that I can create a hash of arrays like this :
Expand|Select|Wrap|Line Numbers
  1. %hash = ( "testA" => [77,88],
  2.                   "testB" => [90, 91] );
  3.  
In this code, each key is associated with a reference to an array.
My question is can I set it up so that each key is associated with
an array - not a reference to an array?

I have tried :
Expand|Select|Wrap|Line Numbers
  1. %hash = ("testA", (77,88),
  2.                  "testB", (90,91) );
  3.  
But it did not work for me.

Thank you - Akino
Jan 21 '15 #1

✓ answered by Kelicula

What is the benefit? Why are you trying to make each key be associated with an array as opposed to a reference to an anonymous array?

3 2238
Kelicula
176 Expert 100+
What is the benefit? Why are you trying to make each key be associated with an array as opposed to a reference to an anonymous array?
Jan 26 '15 #2
Hi Kelicula,

Thank you very much for your reply.

There is no reason for me to try to do that - I was just curious.
Now, I understand that a value in a hash can only be a scalar.

Akino
Jan 26 '15 #3
Kelicula
176 Expert 100+
No problem Akino877!
The way perl overcomes this is through "references", you can use a reference to an array or hash, or even an array of hashes or hash of arrays as a value for a hash, here's an example:
To make a reference you simply use the slash like so:
my @array = ('Jon', 'James', "jimmy', 'Julie');
my $array_ref = \@array;

Now $array_ref is a scalar value that points to the memory allocation spot of the array named @array.

Or you can just make a reference initially with the square brackets:

my $array = ['Jon', 'James', 'jimmy', 'Julie'];

Then
my %hash = ( one => $array, two => $array2, etc... );

To make a reference to an anonymous hash you can use the "{" like so:

my $hash_ref = { one => $array_ref, two => $array_ref2, etc..};

You access these references with the special arrow operator:
$hash->{one}->[1] would equal "James".

Data structures can be very complicated and deep in Perl, but it's all accomplished through references, there are really only 4 types of values in perl; scalar, hash, array, or TYPEGLOB.

There's WAY to much about this to include here, but that should give you some terms to Google with ;-)

Here is a great place to start:
http://perldoc.perl.org/perlref.html

Happy coding...
Jan 26 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array...
34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
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...
7
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ...
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...
38
by: ssg31415926 | last post by:
I need to compare two string arrays defined as string such that the two arrays are equal if the contents of the two are the same, where order doesn't matter and every element must be unique. ...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
5
by: M. Fisher | last post by:
Pardon my ignorance here... I have created arrays such as: var SDET_Lab130= new Array(); SDET_Lab130 = new Array(); SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ;...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.