de-references

From: CJ (enutoneko_at_hotmail.com)
Date: 12/17/03


Date: Wed, 17 Dec 2003 07:05:44 GMT

Perl-folks,

    Got a question that I hope someone can answer. I often use hashs like
structures and they sometimes contain references to other hashs or arrays.
My question is what is the best way to de-reference the internal hash or
array. An example. I recent wrote something that creates a hash which has
a default server name and a reference to an array of other server names.

%server_info = (default_server=>\$default,
                         server_list=> \@servers);

The sub that creates this hash returns a reference to that hash...

return \%server_info;

When I try to use it in the calling program, I have tried to use the
following line only to get syntax errors...

$server_list = sub_called_earlier();

foreach $element (@($server_list->{server_list})) {

        do something with $element;
}

Seems like this should work as $server_list->{server_list} is a reference to
an array. I can get it work by using the following...

$server_array_ref = $server_list->{server_list};
foreach $element (@$server_array_ref) {

        do something with $element;
}

This seems like a real waste of time and there has to be a way to make it
work properly. I have spent time looking for an answer, but have come
up short. If anyone has a reference (no pun intended) to point me to or a
concept on how they make it work, would love to hear it. Thanks.

CJ



Relevant Pages

  • RE: Array de-referencing problem
    ... But I guess it's better to use an array reference? ... Conversation: Array de-referencing problem ... The values of a hash can only be scalars! ...
    (perl.beginners)
  • Re: Complex Objects in Perl
    ... > sub-object is made up of a number of different objects and an array of an ... Objects in Perl are implemented as a reference to an anonymous hash. ...
    (comp.lang.perl)
  • Re: complex data structure
    ... reference to an anonymous one-key hash. ... store, just put them in the array: ... stores a reference to an anonymous 4-element array into scalar variable ...
    (comp.lang.perl.misc)
  • Re: complex data structure
    ... reference to an anonymous one-key hash. ... store, just put them in the array: ... stores a reference to an anonymous 4-element array into scalar variable ...
    (comp.lang.perl)
  • Re: SOLVED: How do I scope a variable if the variable name contains a variable?
    ... Symrefs don't work under use strict. ... This is not a hard reference... ... this is just an array variable. ... Here the members of the hash are references to arrays, ...
    (comp.lang.perl.misc)