Dereferencing ?

J

Joe McGuckin

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
 
G

Gunnar Hjalmarsson

Joe said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top