Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old April 22nd, 2007, 02:55 AM
Bobby Chamness
Guest
 
Posts: n/a
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


  #2  
Old April 22nd, 2007, 04:35 AM
Jürgen Exner
Guest
 
Posts: n/a

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


Bobby Chamness wrote:
Quote:
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.
Quote:
I
want each server to have its own hash.
Just use a hash of (references to) hashes.
Quote:
$server_$myserver_name{$some_key}
Why not
$servers{$myserver_name}{$some_key}

jue


  #3  
Old April 22nd, 2007, 09:55 PM
Joe Smith
Guest
 
Posts: n/a

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


Bobby Chamness wrote:
Quote:
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.
Quote:
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.
Quote:
$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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM