469,916 Members | 1,924 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

de-references

CJ
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
Jul 19 '05 #1
3 1471
CJ wrote:
My question is what is the best way to de-reference the
internal hash or array. [...].

%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})) {
Right idea, wrong style of brackets. Try curlies instead:
foreach $element (@{$server_list->{server_list}}) {

[...] 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.


Please see "perldoc perlref"

jue
Jul 19 '05 #2
"CJ" <en*******@hotmail.com> wrote in message news:<cf*************@twister.socal.rr.com>...
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.
See:
perldoc perlref
perldoc prelreftut
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})) {


When reading Perl code or Perl manuals make sure to use a font in
which {} and () are clearly destinguished. The syntax for
dereferncing a reference that is the result an expression encloses the
expression in {} not ().

This newsgroup does not exist (see FAQ). Please do not start threads
here.
Jul 19 '05 #3
CJ
Jue,

Thank you for the info, have looked thru the perdoc, but guess I missed
it. Thanks again for the help and quick response.

CJ
"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:JG*****************@nwrddc02.gnilink.net...
CJ wrote:
My question is what is the best way to de-reference the
internal hash or array. [...].

%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})) {


Right idea, wrong style of brackets. Try curlies instead:
foreach $element (@{$server_list->{server_list}}) {

[...]
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.


Please see "perldoc perlref"

jue

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Laurent Pointal | last post: by
reply views Thread by Michael Dyson | last post: by
reply views Thread by Alejandro Scomparin | last post: by
1 post views Thread by gandalf | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.