Dereferencing ?

Discussion in 'Perl' started by Joe McGuckin, Nov 28, 2003.

  1. Joe McGuckin

    Joe McGuckin Guest

    If I have the following, how do I get to the members?

    $servers = [{'hostname'=>'secure',
    'description'=>'www'},
    {'hostname'=> 'www3',
    'description'=>'www'},
    {'hostname'=>'www6',
    'description'=>'www'}.
    {'hostname'=>'www10',
    'description'=>'www'},
    {'hostname'=> 'mail',
    'description'=>'mail'}];


    I used to have a simple list of servers and I would say something like:

    foreach $i (@servers)

    But now I want to associate more info with each server entry.

    So, how can I get my list of servers to iterate over?

    Thanks,

    Joe
     
    Joe McGuckin, Nov 28, 2003
    #1
    1. Advertising

  2. Joe McGuckin wrote:
    > If I have the following, how do I get to the members?
    >
    > $servers = [{'hostname'=>'secure',
    > 'description'=>'www'},
    > {'hostname'=> 'www3',
    > 'description'=>'www'},
    > {'hostname'=>'www6',
    > 'description'=>'www'}.
    > {'hostname'=>'www10',
    > 'description'=>'www'},
    > {'hostname'=> 'mail',
    > 'description'=>'mail'}];


    You have a reference to an array of hashes. Assuming that you correct
    the typo, you can print them like this:

    foreach my $server (@$servers) {
    print "Host: $server->{hostname}, Desc: $server->{description}\n";
    }

    > I used to have a simple list of servers and I would say something
    > like:
    >
    > foreach $i (@servers)
    >
    > But now I want to associate more info with each server entry.
    >
    > So, how can I get my list of servers to iterate over?


    Read up on arrays of hashes at
    http://www.perldoc.com/perl5.8.0/pod/perldsc.html

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 28, 2003
    #2
    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. J. Campbell
    Replies:
    4
    Views:
    1,183
    Simon G Best
    Jul 17, 2003
  2. Larry Lindsey
    Replies:
    1
    Views:
    432
    Larry Lindsey
    Sep 24, 2003
  3. Peter L.
    Replies:
    3
    Views:
    386
    Jakob Bieling
    Feb 17, 2004
  4. Martin Zimmermann
    Replies:
    2
    Views:
    495
    Rob Williscroft
    Apr 14, 2004
  5. nifsmith

    Dereferencing pointers

    nifsmith, Oct 19, 2004, in forum: C++
    Replies:
    6
    Views:
    396
    Brian Riis
    Oct 19, 2004
Loading...

Share This Page