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

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:

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
1 2932
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

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

Similar topics

4
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array...
1
by: Andrew Fleet | last post by:
Hi, I'm looking at returning a reference to an array I create within a subroutine. I could do this... sub foo { my @theArray; <snip>
1
by: andreas gammel | last post by:
I want to use Perl hash functionality (hashes of hashes, arrays of hashes, hashes of arrays) in my C programs. Other Perl functions (split, chomp, regular expressions) would be welcome too. ...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
21
by: scandal | last post by:
I am a javascript newbie working on a script that checks whether a "path" from one element in an array to another is "blocked." Currently, the script pushes an already processed cell index (hence...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
6
by: Jake Barnes | last post by:
I was just reading this article on Ajaxian: http://ajaxian.com/archives/show-love-to-the-object-literal This is a newbie question, but what is the object literal? I thought it was like an...
30
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.