Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > adding a variable name to a hash to name is part of the variable name

Reply
Thread Tools

adding a variable name to a hash to name is part of the variable name

 
 
Bobby Chamness
Guest
Posts: n/a
 
      04-22-2007
I have a perl script that I wrote the loops through a list of servers in a
file and I want to create a hash with the server name in it. I want each
server to have its own hash.

for example
$myserver = thor;
hash name
$server_$myserver_name{$some_key}
want to get this hash name
$server_thor_name{$some_key}

How do I get the $myserver name to be interpolated to thor in that line?
I tried this and it failed.
$server_${myserver}_name{$some_key}


-Bobby


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-22-2007
Bobby Chamness wrote:
> I have a perl script that I wrote the loops through a list of servers
> in a file and I want to create a hash with the server name in it.


You mean a the server name as part of the variable name? Well, maybe you
think that's what you want, but it is A Very Bad Idea (TM). See the FAQ "How
can I use a variable as a variable name?" and gazillions of previuos
discussions in CLPM for details.

> I
> want each server to have its own hash.


Just use a hash of (references to) hashes.

> $server_$myserver_name{$some_key}


Why not
$servers{$myserver_name}{$some_key}

jue


 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      04-22-2007
Bobby Chamness wrote:
> I have a perl script that I wrote the loops through a list of servers in a
> file and I want to create a hash with the server name in it.


Sounds good. A master hash with server name as the top-level key.

> I want each server to have its own hash.


With a hash of hashes, each server has its own hash. The server's hash
just doesn't have a name; it is a member of the master hash.

> $server_$myserver_name{$some_key}


$servers{$myserver_name}{$some_key} = $value;


foreach my $server (sort keys %servers) {
print "Processing server $server\n";
foreach my $key (sort keys %{$servers{$server}}) {
print " $key = $servers{$server}{$key}\n";
}
}


-Joe

P.S. The newsgroup comp.lang.perl is defunct. Use comp.lang.perl.misc instead.
 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Adding values to anon hash of hash while looping... adamomitcheney@kiwis.co.uk Perl Misc 5 09-02-2005 04:15 PM
part of variable name is variable, how? bing Perl Misc 2 08-01-2005 09:59 PM
Variable displays at one part while does not in another part in a Jack ASP General 8 05-10-2005 07:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57