Printing array of references to hash maps (perl newbie)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sixtyfootersdude
    New Member
    • Oct 2008
    • 14

    Printing array of references to hash maps (perl newbie)

    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:

    Code:
    	for(my $i=0; $i<@input; $i++){
    		my $hash = $input[$i];
    		print "$i: \n";
    		print "\t".'$hash = ';
    		print $hash;
    		print "\n\t".'${$hash} = ';
    		print ${$hash};
    		print "\n\t";		
    		# print ${$hash}{author}; <- THIS DOES NOT WORK WHY?
    		print "\n";
    		
    	}
    And it prints:

    Code:
    0: 
            $hash = REF(0x180e024)
            ${$hash} = HASH(0x180b3e4)
    
    1: 
            $hash = REF(0x180da94)
            ${$hash} = HASH(0x180b198)
    
    2: 
            $hash = REF(0x180e1c8)
            ${$hash} = HASH(0x180109c)
    
    3: 
            $hash = REF(0x180e21c)
            ${$hash} = HASH(0x180e288)
    
    ....
    I want to print:

    Code:
    0:	author = XXXXXXXXXXX
    # where XXXXXXXXX is the author in the first hash map.
    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.

    Code:
    # inside of a while loop:
    			my $hash = {
    				author => $temp[0],
    				train => $temp[1],
    				fileName => $temp[2],
    			};	
    			push(@input,\$hash);
    Thank you in advance for the help. I appreciate it.
  • sixtyfootersdude
    New Member
    • Oct 2008
    • 14

    #2
    Problem solved.

    ARRRRRG. So stupid. Problem solved.

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

    Ie:

    I was doing:
    Code:
    			push(@input,/$hash);
    Instead of:
    Code:
    			push(@input,$hash);
    Stupid mistake. Thanks for reading.

    Jake

    Comment

    Working...