de-references

Discussion in 'Perl' started by CJ, Dec 17, 2003.

  1. CJ

    CJ Guest

    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
    CJ, Dec 17, 2003
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Dec 17, 2003
    #2
    1. Advertising

  3. CJ

    Guest

    "CJ" <> wrote in message news:<cfTDb.37$>...
    > 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.
    , Dec 17, 2003
    #3
  4. CJ

    CJ Guest

    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" <> wrote in message
    news:JGWDb.20768$...
    > 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
    >
    >
    CJ, Dec 17, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    414
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    434
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    585
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    693
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    819
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page