CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Problems acceding to a hash of hashes....

 



forsola
New User

Feb 19, 2009, 8:58 PM

Post #1 of 2 (1116 views)
Problems acceding to a hash of hashes.... Can't Post

Hi guys,

I am doing a program in perl where the data structure is like:
$my_global{'Networks'} = {};
$my_global{'Networks'}{'values'}[0]='Extranet';
$my_global{'Networks'}{'values'}[1]='Intranet';
$my_global{'Networks'}{'values'}[2]='Monitoring';
$my_global{'Networks'}{'values'}[3]='Management';
$my_global{'Networks'}{'values'}[4]='Localhost';

After all the data structure has defined I can access easily to the values:
print "Array: " . $my_global{'Networks'}{'values'} . "\n";
print "Value 0: " . $my_global{'Networks'}{'values'}[0] . "\n";
print "Value 4: " . $my_global{'Networks'}{'values'}[4] . "\n";

Returns:
Hash: ARRAY(0x813cc68)
Value 0: Extranet
Value 4: Localhost


My problem started when I tried to use functions to access the array values in a bucle. Check this out:

foreach ( ($my_global{'Networks'}{'values'}) ) {
print $_;
}
Returns:
ARRAY(0x86c74e4)

Instead of the 5 values of the array.

Another example this time with hash structures:
$my_global{'Hosts'} = {};
$my_global{'Hosts'}{'OSS1'} = {};
$my_global{'Hosts'}{'OSS1'}{'IpAddresses'} = {};
$my_global{'Hosts'}{'OSS1'}{'IpAddresses'}{'Intranet'} = "192.168.20.5";
$my_global{'Hosts'}{'OSS1'}{'IpAddresses'}{'Extranet'} = "192.168.21.5";
$my_global{'Hosts'}{'OSS1'}{'PhysicalLocation'} = "192.168.1.1";
$my_global{'Hosts'}{'DNS1'} = {};
$my_global{'Hosts'}{'DNS1'}{'IpAddresses'} = {};
$my_global{'Hosts'}{'DNS1'}{'IpAddresses'}{'Intranet'} = "192.168.21.1";


The code:
print "Hash: " . $my_global{'Hosts'}{'OSS1'} . "\n";
print "Intranet: " . $my_global{'Hosts'}{'OSS1'}{'IpAddresses'}{'Intranet'} . "\n";
print "Extranet: " . $my_global{'Hosts'}{'OSS1'}{'IpAddresses'}{'Extranet'} . "\n";

Returns:
Hash: HASH(0xa120e74)
Intranet: 192.168.20.5
Extranet: 192.168.21.5

if I try now to use any hash function I get some errors.

Or if a try to use funtion key to retrieve the hash's keys:
Type of arg 1 to keys must be hash (not hash element) at ./grepGlobal.sh line 57, near "})"
Execution of ./grepGlobal.sh aborted due to compilation errors.

I tried to use references like $$ ${} \$ etc, but I couldn't find a solution. Anybody kind to help me?

Thanks you.

Ferran


KevinR
Veteran


Feb 19, 2009, 10:12 PM

Post #2 of 2 (1114 views)
Re: [forsola] Problems acceding to a hash of hashes.... [In reply to] Can't Post


Code
foreach ( (@{$my_global{'Networks'}{'values'}}) ) {  
print $_;
}

-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives