Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Dereferencing ?

Reply
Thread Tools

Dereferencing ?

 
 
Joe McGuckin
Guest
Posts: n/a
 
      11-28-2003
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-28-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dereferencing pointers nifsmith C++ 6 10-19-2004 03:03 PM
Dereferencing this in constructors initialiser list Martin Zimmermann C++ 2 04-14-2004 02:07 PM
*& dereferencing a pointer to a class Peter L. C++ 3 02-17-2004 01:03 PM
Dereferencing a pointer within a function Larry Lindsey C++ 1 09-24-2003 03:42 PM
dereferencing char array as int array J. Campbell C++ 4 07-17-2003 05:56 PM



Advertisments