End of my tether trying to retrieve keys from multi-dimensional hash

G

guytew

First off, apologies if what I'm trying to parse is not a multi-
dimensional hash - I'm still confused even having read the perldoc and
numerous usenet postings :-(

I am using a perl module to store various configurations [see snippet
below]. I want to be able to create an array called @sids that
contains the keys ZA1, YB4, XMM so that I can loop through the array
and retrieve Description, Ports or whatever using. It seems all the
variations for retrieving keys from the simple
keys %Instances to the convoluted display nothing! I'm successfully
using the Config when I know the keys, but now I need to build a list
to work from. Any solutions gratefully received.

foreach $sid (@SIDS) {
my $port = $BASIS::Config::Instances{$sid}{Ports}
# do something with the retrieved value
}


[ snippet from my Config package ]

package BASIS::Config;
use strict;
use vars qw( %Alerts %Instances %Servers );

%Instances = (
ZA1 => {
Description => "Production",
Ports => '3300 3200',
},
YB4 => {
Description => "Quality",
Ports => '3300 3200',
},
XMM => {
Description => "Development",
Ports => '3300 3200',
},
);
 
B

Ben Morrow

Quoth (e-mail address removed):
First off, apologies if what I'm trying to parse is not a multi-
dimensional hash - I'm still confused even having read the perldoc and
numerous usenet postings :-(

I am using a perl module to store various configurations [see snippet
below]. I want to be able to create an array called @sids that
contains the keys ZA1, YB4, XMM so that I can loop through the array
and retrieve Description, Ports or whatever using. It seems all the
variations for retrieving keys from the simple
keys %Instances to the convoluted display nothing! I'm successfully
using the Config when I know the keys, but now I need to build a list
to work from. Any solutions gratefully received.

foreach $sid (@SIDS) {
my $port = $BASIS::Config::Instances{$sid}{Ports}
# do something with the retrieved value

$port is a hashref, so you need to apply 'Use Rule 1' from perlreftut:

# If I had an ordinary hash, I would extract the keys like this

keys %hash

# Replace the name of the hash with a block

keys %{ }

# Put the hashref inside

keys %{ $port }

This will do what you want. When you're comfortable with the idea, you
can shorten that into

keys %$port

since $port is a simple scalar; but remember it's just a shorthand. You
could also use something like

keys %{ $BASIS::Config::Instances{$sid}{Ports} }

without the temporary, but that's just ugly :). In this case the {}
*aren't* optional, as what's inside is too complicated.

Ben
 
W

woodyg

Quoth (e-mail address removed):




First off, apologies if what I'm trying to parse is not a multi-
dimensional hash - I'm still confused even having read the perldoc and
numerous usenet postings :-(
I am using a perl module to store various configurations [see snippet
below].  I want to be able to create an array called @sids that
contains the keys ZA1, YB4, XMM so that I can loop through the array
and retrieve Description, Ports or whatever using.  It seems all the
variations for retrieving keys from the simple
keys %Instances to the convoluted display nothing!  I'm successfully
using the Config when I know the keys, but now I need to build a list
to work from.  Any solutions gratefully received.
foreach $sid (@SIDS) {
   my $port = $BASIS::Config::Instances{$sid}{Ports}
   # do something with the retrieved value

$port is a hashref, so you need to apply 'Use Rule 1' from perlreftut:

    # If I had an ordinary hash, I would extract the keys like this

    keys %hash

    # Replace the name of the hash with a block

    keys %{ }

    # Put the hashref inside

    keys %{ $port }

This will do what you want. When you're comfortable with the idea, you
can shorten that into

    keys %$port

since $port is a simple scalar; but remember it's just a shorthand. You
could also use something like

    keys %{ $BASIS::Config::Instances{$sid}{Ports} }

without the temporary, but that's just ugly :). In this case the {}
*aren't* optional, as what's inside is too complicated.

Ben- Hide quoted text -

- Show quoted text -

Apologies, Ben, but I couldn't understand how I was going to get the
key names (ZA1,YB4,XMM) using your explanation. I'm trying to find
out how to discover the key names from the %Instances hash. If I use
the following I can retrieve the values, but then I have to hard code
them in the @sids array.

use BASIS::Config qw:)Instances);

@sids = qw(ZA1 YB4 XMM);

foreach $sid (@sids) {
print "$sid\t";
print "$BASIS::Config::Instances{$sid}{Description}\t";
print "$BASIS::Config::Instances{$sid}{Ports}\n";
}

which produces the following output:-

ZA1 Production 3300 3200
YB4 Quality 3300 3200
XMM Development 3300 3200


Guy
 
J

John W. Krahn

woodyg said:
Apologies, Ben, but I couldn't understand how I was going to get the
key names (ZA1,YB4,XMM) using your explanation. I'm trying to find
out how to discover the key names from the %Instances hash. If I use
the following I can retrieve the values, but then I have to hard code
them in the @sids array.

use BASIS::Config qw:)Instances);

@sids = qw(ZA1 YB4 XMM);

foreach $sid (@sids) {
print "$sid\t";
print "$BASIS::Config::Instances{$sid}{Description}\t";
print "$BASIS::Config::Instances{$sid}{Ports}\n";
}

which produces the following output:-

ZA1 Production 3300 3200
YB4 Quality 3300 3200
XMM Development 3300 3200

foreach my $sid ( keys %BASIS::Config::Instances ) {
print "$sid\t";
print "$BASIS::Config::Instances{$sid}{Description}\t";
print "$BASIS::Config::Instances{$sid}{Ports}\n";
}



John
 
W

woodyg

foreach my $sid ( keys %BASIS::Config::Instances ) {
    print "$sid\t";
    print "$BASIS::Config::Instances{$sid}{Description}\t";
    print "$BASIS::Config::Instances{$sid}{Ports}\n";

}

John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall- Hide quoted text -

- Show quoted text -

Cheers John. That worked a treat.
 

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

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top