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

Printing array of references to hash maps (perl newbie)

P: 14
Good Morning!

I am a perl newbie and I think that I am struggling with references.

I have an array of references to hashes which I am trying to print. This is what I have:

Expand|Select|Wrap|Line Numbers
  1.     for(my $i=0; $i<@input; $i++){
  2.         my $hash = $input[$i];
  3.         print "$i: \n";
  4.         print "\t".'$hash = ';
  5.         print $hash;
  6.         print "\n\t".'${$hash} = ';
  7.         print ${$hash};
  8.         print "\n\t";        
  9.         # print ${$hash}{author}; <- THIS DOES NOT WORK WHY?
  10.         print "\n";
  11.  
  12.     }
  13.  
And it prints:

Expand|Select|Wrap|Line Numbers
  1. 0: 
  2.         $hash = REF(0x180e024)
  3.         ${$hash} = HASH(0x180b3e4)
  4.  
  5. 1: 
  6.         $hash = REF(0x180da94)
  7.         ${$hash} = HASH(0x180b198)
  8.  
  9. 2: 
  10.         $hash = REF(0x180e1c8)
  11.         ${$hash} = HASH(0x180109c)
  12.  
  13. 3: 
  14.         $hash = REF(0x180e21c)
  15.         ${$hash} = HASH(0x180e288)
  16.  
  17. ....
  18.  
I want to print:

Expand|Select|Wrap|Line Numbers
  1. 0:    author = XXXXXXXXXXX
  2. # where XXXXXXXXX is the author in the first hash map.  
  3.  
I am not sure if this is enough info to tell you what is happening. I guess I should include the code for where I am filling the array of ref to hashes.

Expand|Select|Wrap|Line Numbers
  1. # inside of a while loop:
  2.             my $hash = {
  3.                 author => $temp[0],
  4.                 train => $temp[1],
  5.                 fileName => $temp[2],
  6.             };    
  7.             push(@input,\$hash);
  8.  
Thank you in advance for the help. I appreciate it.
Nov 15 '09 #1
Share this Question
Share on Google+
1 Reply


P: 14
ARRRRRG. So stupid. Problem solved.

When I was adding my hashes to the arrays I was adding references to references.

Ie:

I was doing:
Expand|Select|Wrap|Line Numbers
  1.             push(@input,/$hash);
  2.  
Instead of:
Expand|Select|Wrap|Line Numbers
  1.             push(@input,$hash);
  2.  
Stupid mistake. Thanks for reading.

Jake
Nov 15 '09 #2

Post your reply

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