By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,692 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,692 IT Pros & Developers. It's quick & easy.

array in perl

P: 5
Can some one help me to understand the below perl code line:
Expand|Select|Wrap|Line Numbers
  1. 1. my @myarray;
  2.  
  3. 2. $myarray[0][0] {testscore} =0;
  4.  
  5. 3. $myarray[0][0] {testgrade} = "none";
  6.  
I understand the code line #1, it is basically declaring the array.

Can some one help me with the meaning of code line#2 and 3.

Thanks.
Feb 19 '10 #1

✓ answered by RonB

#2 and #3 are referred to as a hash of hashes.
No, they are not.

#2 and #3 are AoAoH (an array of arrays of hashes).

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7. my @myarray;
  8. $myarray[0][0] {testscore} =0;
  9. $myarray[0][0] {testgrade} = "none";
  10.  
  11. print Dumper \@myarray;
Expand|Select|Wrap|Line Numbers
  1. C:\TEMP>test.pl
  2. $VAR1 = [
  3.           [
  4.             {
  5.               'testgrade' => 'none',
  6.               'testscore' => 0
  7.             }
  8.           ]
  9.         ];

Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Since you know what #1 is I won't go into that. But please know that these are not arrays, these are hashes. To be valid though for #2 and #3, #1 would actually have to be:

Expand|Select|Wrap|Line Numbers
  1. my %myhash;
  2.  
#2 and #3 are referred to as a hash of hashes. Read this link to get a better idea.

Also, please use code tags around any code you enter in the forums.

Regards,

Jeff
Feb 19 '10 #2

Expert Mod 100+
P: 589
#2 and #3 are referred to as a hash of hashes.
No, they are not.

#2 and #3 are AoAoH (an array of arrays of hashes).

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7. my @myarray;
  8. $myarray[0][0] {testscore} =0;
  9. $myarray[0][0] {testgrade} = "none";
  10.  
  11. print Dumper \@myarray;
Expand|Select|Wrap|Line Numbers
  1. C:\TEMP>test.pl
  2. $VAR1 = [
  3.           [
  4.             {
  5.               'testgrade' => 'none',
  6.               'testscore' => 0
  7.             }
  8.           ]
  9.         ];
Feb 19 '10 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
You are completely correct. I should actually wake up before replying I guess. :)

Completely missed the brackets signifying the arrays. UGH!!

Regards,

Jeff
Feb 19 '10 #4

Post your reply

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