hash / array / hash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keithl
    New Member
    • May 2007
    • 8

    hash / array / hash

    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)

    [CODE=perl]
    use XML::Simple;
    my $xml_hash = XMLin('concorde .xml');
    # use Data::Dumper;
    # print Dumper($xml_has h);

    print "first hash : $xml_hash\n";

    for my $key (keys %{$xml_hash}) {
    print "$key => ${$xml_hash}{$k ey}\n";
    #output from above is a single key of "ORDER"
    }

    print "test0: ${$xml_hash}{OR DER}[0]\n";
    print "test1: ${$xml_hash}{OR DER}[1]\n";

    my $next_hash = "${$xml_hash}{O RDER}[0]";
    print "next hash : $next_hash \n";
    for my $key (keys %{$next_hash}) {
    print "$key => ${$next_hash}{$ key}\n";
    }
    [/CODE]

    OK.. the output of the above is:

    Code:
    first hash : HASH(0x304867a8)
    ORDER => ARRAY(0x304b00d0)
    test0: HASH(0x30486c1c)
    test1: HASH(0x30486d54)
    next hash : HASH(0x30486c1c)
    Something I have done is clearly wrong/stupid etc.

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

    Keith
    Last edited by miller; Jun 19 '07, 04:16 PM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    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:

    [CODE=perl]
    my $next_hash = "${$xml_hash}{O RDER}[0]";
    [/CODE]

    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:

    [CODE=perl]
    my $next_hash = $xml_hash->{ORDER}[0];
    [/CODE]

    - Miller

    Comment

    • keithl
      New Member
      • May 2007
      • 8

      #3
      Thanks Milller...

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

      Keith

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        I understand. You welcome :)

        - Miller

        Comment

        Working...