Connecting Tech Pros Worldwide Forums | Help | Site Map

hash / array / hash

Newbie
 
Join Date: May 2007
Posts: 8
#1: Jun 19 '07
Hi (again !)

I posted a question yesterday just for info on hashrefs which I have read and it makes sense. Putting this into proactive though has now toally confused me !

I have an XML file sucked into a hash via XML::Simple. That's OK. I can see the data using Data::Dumper.

I seem to have a Hash of Arrays, and those Arrays contain another hash.
All I am trying to do is get to the data but every route I take I am getting myself into more confusion.

I have copied a chunk of the code in bwloe so you can tell me what I am missing (and laugh a bit at the same time at a novice !! lol)

Expand|Select|Wrap|Line Numbers
  1. use XML::Simple;
  2. my $xml_hash = XMLin('concorde.xml');
  3. # use Data::Dumper;
  4. # print Dumper($xml_hash);
  5.  
  6. print "first hash : $xml_hash\n";
  7.  
  8. for my $key (keys %{$xml_hash}) {
  9.     print "$key => ${$xml_hash}{$key}\n";
  10.     #output from above is a single key of "ORDER"
  11. }
  12.  
  13. print "test0: ${$xml_hash}{ORDER}[0]\n";
  14. print "test1: ${$xml_hash}{ORDER}[1]\n";
  15.  
  16. my $next_hash = "${$xml_hash}{ORDER}[0]";
  17. print "next hash : $next_hash \n";
  18. for my $key (keys %{$next_hash}) {
  19.     print "$key => ${$next_hash}{$key}\n";
  20. }
  21.  
OK.. the output of the above is:

Expand|Select|Wrap|Line Numbers
  1. first hash : HASH(0x304867a8)
  2. ORDER => ARRAY(0x304b00d0)
  3. test0: HASH(0x30486c1c)
  4. test1: HASH(0x30486d54)
  5. next hash : HASH(0x30486c1c)
  6.  
Something I have done is clearly wrong/stupid etc.

Help !! Once I get one working I'll be fine !

Keith

miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#2: Jun 19 '07

re: hash / array / hash


Greetings Keith,

It appears that you have some familiarity with complex data structures. However, just note that the one tutorial that you just be familiar with when manipulating said structures is the following:

perldoc perldsc Data Structures Cookbook

Anyway, your problem lies in the following assignment:

Expand|Select|Wrap|Line Numbers
  1. my $next_hash = "${$xml_hash}{ORDER}[0]";
  2.  
Your assigning $next_hash to the string representation of the hash reference at that location, not the actual reference. Also, it's simply a stylistic difference, but I would refer to that variable like the following:

Expand|Select|Wrap|Line Numbers
  1. my $next_hash = $xml_hash->{ORDER}[0];
  2.  
- Miller
Newbie
 
Join Date: May 2007
Posts: 8
#3: Jun 20 '07

re: hash / array / hash


Thanks Milller...

That moved me on past the step I was screaming at !!

Keith
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#4: Jun 20 '07

re: hash / array / hash


I understand. You welcome :)

- Miller
Reply