473,385 Members | 1,582 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,385 software developers and data experts.

How to access an element in a hash of references?

Hi ,
I am new to Perl and have some trouble in accessing elements stored in a hash. For example, if I have a has structure with the following data elements

Expand|Select|Wrap|Line Numbers
  1. rank_filename = (
  2.     '535.714285714286' => [
  3.         [2,'535.714285714286','3','5','rep_Ara_2','Arav'],
  4.         [3,'535.714285714286','4','5','rep_Ara_12','Ara']
  5.     ],
  6.     '642.857142857143' => [
  7.         [1,'642.857142857143','3','6','rep_Ara_1','Ara']
  8.     ]
  9. );
  10.  
how do I access the elements in the list stored in the hash? Can I access the array inside each key in the hash and store them into another array?

Thanks in advance!
Mar 7 '07 #1
6 3633
KevinADC
4,059 Expert 2GB
assuming rank_filename is a reference itself, here is how you could loop through the whole data structure:

Expand|Select|Wrap|Line Numbers
  1. foreach my $key ( keys %{ $rank_filename } ) {
  2.    foreach my $i ( @{ $rank_filename{$key} } ) {
  3.       print $i,"\n";
  4.    }
this link should help also:

http://perldoc.perl.org/index-tutorials.html

the first three tutorials discuss references.
Mar 8 '07 #2
assuming rank_filename is a reference itself, here is how you could loop through the whole data structure:

Expand|Select|Wrap|Line Numbers
  1. foreach my $key ( keys %{ $rank_filename } ) {
  2.    foreach my $i ( @{ $rank_filename{$key} } ) {
  3.       print $i,"\n";
  4.    }
this link should help also:

http://perldoc.perl.org/index-tutorials.html

the first three tutorials discuss references.
Hi, thanks for the reply but when I try to print the whole array it just prints the array addresses. This is the output I get when I printed it out

ARRAY(0x9936f48)
ARRAY(0x9e6a760)

I need to access the arrays inside the array which is stored as a value for the key in the hash. Typecasting it to an array gives me compilation errors. Is there any other way?
Thanks.
Mar 8 '07 #3
miller
1,089 Expert 1GB
You should spend some time studying the perldoc tutorial on complex data structures:

perldoc Perl Data Structures Cookbook

However, expanding on Kevin's example a little bit, and assuming rank_filename is actually a hash (you're missing the type cast prefix), then a way to toggle through all of this structure would be like this:

Expand|Select|Wrap|Line Numbers
  1. my %rank_filename = (
  2.     '535.714285714286' => [
  3.         [2,'535.714285714286','3','5','rep_Ara_2','Arav'],
  4.         [3,'535.714285714286','4','5','rep_Ara_12','Ara']
  5.     ],
  6.     '642.857142857143' => [
  7.         [1,'642.857142857143','3','6','rep_Ara_1','Ara']
  8.     ]
  9. );
  10.  
  11. foreach my $key ( keys %rank_filename ) {
  12.     for (my $i=0; $i < @{ $rank_filename{$key} }; $i++) {
  13.         for (my $j = 0; $j < @{ $rank_filename{$key}[$i] }; $j++) {
  14.             print "$key, $i, $j = $rank_filename{$key}[$i][$j]\n";
  15.         }
  16.     }
  17. }
  18.  
The output is as follows:

Expand|Select|Wrap|Line Numbers
  1. 535.714285714286, 0, 0 = 2
  2. 535.714285714286, 0, 1 = 535.714285714286
  3. 535.714285714286, 0, 2 = 3
  4. 535.714285714286, 0, 3 = 5
  5. 535.714285714286, 0, 4 = rep_Ara_2
  6. 535.714285714286, 0, 5 = Arav
  7. 535.714285714286, 1, 0 = 3
  8. 535.714285714286, 1, 1 = 535.714285714286
  9. 535.714285714286, 1, 2 = 4
  10. 535.714285714286, 1, 3 = 5
  11. 535.714285714286, 1, 4 = rep_Ara_12
  12. 535.714285714286, 1, 5 = Ara
  13. 642.857142857143, 0, 0 = 1
  14. 642.857142857143, 0, 1 = 642.857142857143
  15. 642.857142857143, 0, 2 = 3
  16. 642.857142857143, 0, 3 = 6
  17. 642.857142857143, 0, 4 = rep_Ara_1
  18. 642.857142857143, 0, 5 = Ara
  19.  
Note, there are plenty of more efficient ways to traverse this data structure using each and foreach. But this method demonstrates how to travel deeply into a structure and still dereference and array both in its entirety and by element.

- Miller
Mar 8 '07 #4
KevinADC
4,059 Expert 2GB
I see I missed one level of the arrays, the structure you posted is a hash of arrays of arrays. Assuming like MIller it's a hash instead of a reference to a hash, here is a simplifid version of printing the arrays:

Expand|Select|Wrap|Line Numbers
  1. %rank_filename = (
  2.     '535.714285714286' => [
  3.         [2,'535.714285714286','3','5','rep_Ara_2','Arav'],
  4.         [3,'535.714285714286','4','5','rep_Ara_12','Ara']
  5.     ],
  6.     '642.857142857143' => [
  7.         [1,'642.857142857143','3','6','rep_Ara_1','Ara']
  8.     ]
  9. );
  10.  
  11. foreach my $key ( keys %rank_filename ) {
  12.    foreach my $i ( @{ $rank_filename{$key} } ) {
  13.       print join(',',@$i),"\n";
  14.    }
  15.  
output:

2,535.714285714286,3,5,rep_Ara_2,Arav
3,535.714285714286,4,5,rep_Ara_12,Ara
1,642.857142857143,3,6,rep_Ara_1,Ara
Mar 8 '07 #5
I see I missed one level of the arrays, the structure you posted is a hash of arrays of arrays. Assuming like MIller it's a hash instead of a reference to a hash, here is a simplifid version of printing the arrays:

Expand|Select|Wrap|Line Numbers
  1. %rank_filename = (
  2.     '535.714285714286' => [
  3.         [2,'535.714285714286','3','5','rep_Ara_2','Arav'],
  4.         [3,'535.714285714286','4','5','rep_Ara_12','Ara']
  5.     ],
  6.     '642.857142857143' => [
  7.         [1,'642.857142857143','3','6','rep_Ara_1','Ara']
  8.     ]
  9. );
  10.  
  11. foreach my $key ( keys %rank_filename ) {
  12.    foreach my $i ( @{ $rank_filename{$key} } ) {
  13.       print join(',',@$i),"\n";
  14.    }
  15.  
output:

2,535.714285714286,3,5,rep_Ara_2,Arav
3,535.714285714286,4,5,rep_Ara_12,Ara
1,642.857142857143,3,6,rep_Ara_1,Ara
Thank you for the reply. I think this is what I was looking for. Does @$ mean type casting the scalar variable to an array? Thank you once again.
Mar 8 '07 #6
KevinADC
4,059 Expert 2GB
it means dereferencing the array reference. I guess you could think of it either way though.
Mar 8 '07 #7

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

Similar topics

2
by: pillbug | last post by:
Hi, I'm trying to tie a hash that is returned from a DBI method called fetchrow_hashref. All I want to do is override the FETCH method to validate my key names. But fetchrow_hashref returns...
2
by: Me | last post by:
I have a hash (of a hashes) that has data like this table1->table_name ->table_size table2->table_name ->table_size
4
by: Ragnar Heil | last post by:
This line works fine for me: Set objDOMnode = objDom.selectSingleNode("//headline") strHeadline = objDOMnode.Text Now I want to get the value from this node: <nitf> - <head> - <docdata> -...
3
by: SAM | last post by:
Hi everyone, I consider myself a very competent programmer when it comes to actual programming and analyzing the business that I'm modelling. I am now crossing into what I would consider Access...
3
by: Lauren Wilson | last post by:
Hello good folks! It is WAY past time for me to figure out how to check for the existence of all referenced libraries on application startup and provide feedback to the user if they are not. ...
4
by: Bo Peng | last post by:
Dear list, I am looking for a way to store a large amount of unique sequences that will be accessed by objects. The most important operations are: 1. Direct access to the sequences (from...
6
by: fdmfdmfdm | last post by:
This might not be the best place to post this topic, but I assume most of the experts in C shall know this. This is an interview question. My answer is: hash table gives you O(1) searching but...
2
by: CodeTilYaDrop | last post by:
Hello, I can not figure this code out! I have a function that needs to access an element of the the linked list as an argument. Could someone give me an example of the syntax? Do you have any...
3
by: Lance Wynn | last post by:
Hello, I am receiving this error when trying to instantiate a webservice component. I have 2 development machines, both are XP sp2 with VS 2008 installed. On one machine, the code works fine. On...
2
nathj
by: nathj | last post by:
Hi, I'm trying to develop what should be a simple jsp that reads an XML file, parses it and displays the content in a web page. Simple right? I have developed the XML and so any changes can be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.