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 -
rank_filename = (
-
'535.714285714286' => [
-
[2,'535.714285714286','3','5','rep_Ara_2','Arav'],
-
[3,'535.714285714286','4','5','rep_Ara_12','Ara']
-
],
-
'642.857142857143' => [
-
[1,'642.857142857143','3','6','rep_Ara_1','Ara']
-
]
-
);
-
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!
6 3633
assuming rank_filename is a reference itself, here is how you could loop through the whole data structure: - foreach my $key ( keys %{ $rank_filename } ) {
-
foreach my $i ( @{ $rank_filename{$key} } ) {
-
print $i,"\n";
-
}
-
}
this link should help also: http://perldoc.perl.org/index-tutorials.html
the first three tutorials discuss references.
assuming rank_filename is a reference itself, here is how you could loop through the whole data structure: - foreach my $key ( keys %{ $rank_filename } ) {
-
foreach my $i ( @{ $rank_filename{$key} } ) {
-
print $i,"\n";
-
}
-
}
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.
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: -
my %rank_filename = (
-
'535.714285714286' => [
-
[2,'535.714285714286','3','5','rep_Ara_2','Arav'],
-
[3,'535.714285714286','4','5','rep_Ara_12','Ara']
-
],
-
'642.857142857143' => [
-
[1,'642.857142857143','3','6','rep_Ara_1','Ara']
-
]
-
);
-
-
foreach my $key ( keys %rank_filename ) {
-
for (my $i=0; $i < @{ $rank_filename{$key} }; $i++) {
-
for (my $j = 0; $j < @{ $rank_filename{$key}[$i] }; $j++) {
-
print "$key, $i, $j = $rank_filename{$key}[$i][$j]\n";
-
}
-
}
-
}
-
The output is as follows: -
535.714285714286, 0, 0 = 2
-
535.714285714286, 0, 1 = 535.714285714286
-
535.714285714286, 0, 2 = 3
-
535.714285714286, 0, 3 = 5
-
535.714285714286, 0, 4 = rep_Ara_2
-
535.714285714286, 0, 5 = Arav
-
535.714285714286, 1, 0 = 3
-
535.714285714286, 1, 1 = 535.714285714286
-
535.714285714286, 1, 2 = 4
-
535.714285714286, 1, 3 = 5
-
535.714285714286, 1, 4 = rep_Ara_12
-
535.714285714286, 1, 5 = Ara
-
642.857142857143, 0, 0 = 1
-
642.857142857143, 0, 1 = 642.857142857143
-
642.857142857143, 0, 2 = 3
-
642.857142857143, 0, 3 = 6
-
642.857142857143, 0, 4 = rep_Ara_1
-
642.857142857143, 0, 5 = Ara
-
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
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: -
%rank_filename = (
-
'535.714285714286' => [
-
[2,'535.714285714286','3','5','rep_Ara_2','Arav'],
-
[3,'535.714285714286','4','5','rep_Ara_12','Ara']
-
],
-
'642.857142857143' => [
-
[1,'642.857142857143','3','6','rep_Ara_1','Ara']
-
]
-
);
-
-
foreach my $key ( keys %rank_filename ) {
-
foreach my $i ( @{ $rank_filename{$key} } ) {
-
print join(',',@$i),"\n";
-
}
-
}
-
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
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: -
%rank_filename = (
-
'535.714285714286' => [
-
[2,'535.714285714286','3','5','rep_Ara_2','Arav'],
-
[3,'535.714285714286','4','5','rep_Ara_12','Ara']
-
],
-
'642.857142857143' => [
-
[1,'642.857142857143','3','6','rep_Ara_1','Ara']
-
]
-
);
-
-
foreach my $key ( keys %rank_filename ) {
-
foreach my $i ( @{ $rank_filename{$key} } ) {
-
print join(',',@$i),"\n";
-
}
-
}
-
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.
it means dereferencing the array reference. I guess you could think of it either way though.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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>
-...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |