473,396 Members | 1,738 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,396 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 2255
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.