Dereferencing ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe McGuckin

    Dereferencing ?

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

    $servers = [{'hostname'=>'s ecure',
    'description'=> 'www'},
    {'hostname'=> 'www3',
    'description'=> 'www'},
    {'hostname'=>'w ww6',
    'description'=> 'www'}.
    {'hostname'=>'w ww10',
    '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
  • Gunnar Hjalmarsson

    #2
    Re: Dereferencing ?

    Joe McGuckin wrote:[color=blue]
    > If I have the following, how do I get to the members?
    >
    > $servers = [{'hostname'=>'s ecure',
    > 'description'=> 'www'},
    > {'hostname'=> 'www3',
    > 'description'=> 'www'},
    > {'hostname'=>'w ww6',
    > 'description'=> 'www'}.
    > {'hostname'=>'w ww10',
    > 'description'=> 'www'},
    > {'hostname'=> 'mail',
    > 'description'=> 'mail'}];[/color]

    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";
    }
    [color=blue]
    > 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?[/color]

    Read up on arrays of hashes at


    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    Comment

    Working...