Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've constructed a rather complex data structure, after reading in the perl book panther perl book I wanted to give it a shot (and using perl for 6 months )
I have a hash that used as a key a servername the value of the hash is a reference to an array that has 2 elements those are again 2 references to arrays the dump look like this,
the first one is a normal list the second one is again an array of arrays but I seem to have problems with my references.


I can traverse the code in a following mock up code.
<some make up code> while(($key, $value) = each(%hservers)) { $hserver{$keyX}->[0]->[1] ="some value"; $hserver{$keyX}->[1]->[0]->[0] ="1"; #secondpart $hserver{$keyX}->[1]->[0]->[1] =1; #secondpart $hserver{$keyX}->[1]->[0]->[2] ="parX"; #secondpart } </some make up code>
In this way I can build the structure completely the second part of the structure is build dynamically by my input/gui thingy :-).

Therefore the second part should be constructed by an init function.
My idea was to make a function and give the reference as argument.
And I need to be able to access the values later on too to add stuff , remove stuff from the structure by just passing the reference of the hash.

But in the init function I am not able to construct the code to traverse the hash and init it.
while(($key, $value) = each(%$href)) { %$href{$key}->[1]->[0]->[0] = 1; #this gives a syntax error %{$href{$key}}->[1]->[0]->[0] = "blah" #gives error print "$key\n"; # but when i do this the code prints all the keys o +f the reference to the hash. so I assume the the hash was passed thr +ough correctly }
Any pointers where I went wrong ?

dump of the hash

$VAR1 = 'server1'; $VAR2 = [ [ '', '4', '0', '0', '-1', '0', '', '0' ], [ [ 1, 1, 'par 2' ], [ 2, 999, 'par 1' ] ] ]; $VAR3 = 'server2'; $VAR4 = [ [ '', '2', '0', '0', '-1', '0', '', '0' ], [ [ 1, 1, 'par 2' ], [ 2, 999, 'par 1' ] ] ]; $VAR5 = 'server3'; $VAR6 = [ [ '', '3', '0', '0', '-1', '0', '', '0' ], [ [ 1, 1, 'par 2' ], [ 2, 999, 'par 1' ] ] ];

CODE and READMORE tags added by Arunbear

Replies are listed 'Best First'.
Re: references troubles
by philcrow (Priest) on Sep 22, 2005 at 18:56 UTC
    When refering to elements of hash, don't use the % sigil. In other words say something like:
    $href->{name} = $value;
    When working with hashes use braces {}, not brackets [].

    Finally, you can omit all the -> except the first one:

    $href->{name}{subkey}
    Phil
Re: references troubles
by graff (Chancellor) on Sep 22, 2005 at 19:34 UTC
    This should work also -- the "$$href" part means that you are dereferencing $href and looking to identify a scalar value somewhere in the referenced structure; this way, you get to avoid using "->" altogether:
    while(($key, $value) = each(%$href)) { $$href{$key}[1][0][0] = 1; ... print "$key/1/0/0 = $$href{$key}[1][0][0]\n"; }
    Note that if you wanted to refer to an entire array that is referenced in the structure, you need to use extra curly brackets around the expression that follows the @ sigil:
    my @toparray = @{$$href{$key}}; my @midarray = @{$$href{$key}[1]}; my @endarray = @{$$href{$key}[1][0]};
Re: references troubles
by ysth (Canon) on Sep 23, 2005 at 06:05 UTC
    Any pointers where I went wrong ?
    Assuming '%' is needed to dereference a hash reference. Instead, you just need to put the hash reference (e.g. $href) where you would normally put the name of the hash (where "name" means not including the $,@,% sigils). So where you'd say $hash{key}, you say $$href{key} (though $href->{key} also does the trick). See references quick reference for a quick runthrough of the possibilities.