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

Discussion in 'Perl Misc' started by guytew@googlemail.com, Mar 13, 2008.

  1. Guest

    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',
    },
    );
    , Mar 13, 2008
    #1
    1. Advertising

  2. Ben Morrow Guest

    Quoth :
    > 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
    Ben Morrow, Mar 13, 2008
    #2
    1. Advertising

  3. woodyg Guest

    Re: End of my tether trying to retrieve keys from multi-dimensionalhash

    On Mar 13, 2:34 pm, Ben Morrow <> wrote:
    > Quoth :
    >
    >
    >
    >
    >
    > > 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
    woodyg, Mar 13, 2008
    #3
  4. Re: End of my tether trying to retrieve keys from multi-dimensionalhash

    woodyg wrote:
    >
    > 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
    --
    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
    John W. Krahn, Mar 13, 2008
    #4
  5. woodyg Guest

    Re: End of my tether trying to retrieve keys from multi-dimensionalhash

    > 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.
    woodyg, Mar 13, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    513
    red floyd
    Nov 10, 2011
  2. Wirianto Djunaidi
    Replies:
    2
    Views:
    200
    Wirianto Djunaidi
    Apr 29, 2008
  3. David Sainte-claire

    Dynamically creating a multi dimensional hash

    David Sainte-claire, Aug 11, 2009, in forum: Ruby
    Replies:
    2
    Views:
    404
    Joel VanderWerf
    Aug 11, 2009
  4. ifiaz

    Sorting a multi-dimensional hash

    ifiaz, Nov 11, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    177
    Tad McClellan
    Nov 13, 2003
  5. samasama
    Replies:
    4
    Views:
    115
    samasama
    Sep 20, 2006
Loading...

Share This Page