Hash reference question

Discussion in 'Perl Misc' started by Kristofer Pettijohn, Aug 8, 2004.

  1. I'm defining a hash similiar to what follows:

    my $HASH = (
    'key1' => (
    'sub1' => 'key1value1',
    'sub2' => 'key1value2'
    ),
    'key2' => (
    'sub1' => 'key2value1',
    'sub2' => 'key2value2')
    )

    and I would like to pass the reference of $HASH{'key1'} to a sub. How
    do I go about doing this?

    Thanks!

    --
    Kristofer Pettijohn
    Kristofer Pettijohn, Aug 8, 2004
    #1
    1. Advertising

  2. Kristofer Pettijohn

    ChrisO Guest

    Kristofer Pettijohn wrote:
    > I'm defining a hash similiar to what follows:
    >
    > my $HASH = (
    > 'key1' => (
    > 'sub1' => 'key1value1',
    > 'sub2' => 'key1value2'
    > ),
    > 'key2' => (
    > 'sub1' => 'key2value1',
    > 'sub2' => 'key2value2')
    > )
    >
    > and I would like to pass the reference of $HASH{'key1'} to a sub. How
    > do I go about doing this?
    >


    First of all, define the hash properly using matching braces and not
    parens and end with a semicolon:

    my $hash = {
    key1 => {
    sub1 => 'key1value1',
    sub2 => 'key1value2',
    },
    key2 => {
    sub1 => 'key2value1',
    sub2 => 'key2value2',
    },
    };

    The quoting of the keys is not required unless you are going to include
    whitespace in your key values.

    To pass the key1 reference to a sub, just reference it:

    subroutine( $hash->{key1} );
    sub subroutine { print "$_[0]->{sub1}\n" }

    -ceo
    ChrisO, Aug 8, 2004
    #2
    1. Advertising

  3. ChrisO <> wrote:
    > First of all, define the hash properly using matching braces and not
    > parens and end with a semicolon:


    I do -- I guess I made the mistake of using parens instead of braces..
    but the semicolons are definitely in the code -- I just typed up a short
    un-tested example for my question.

    Thanks for your help though - I understand it now.

    --
    Kristofer Pettijohn
    Kristofer Pettijohn, Aug 8, 2004
    #3
  4. Kristofer Pettijohn

    Uri Guttman Guest

    >>>>> "SM" == Steve May <> writes:

    >> The quoting of the keys is not required unless you are going to
    >> include whitespace in your key values.
    >>


    SM> Ah.... that is not totally correct. When warnings are on and using
    SM> strict, a key like this-is-a-key will *not* compile.

    he should have said when using => you don't need to quote simple word
    keys (a leading - is allowed because too many packages use -foo as
    args). pretty much anything else needs normal quoting.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Aug 9, 2004
    #4
  5. Kristofer Pettijohn

    ChrisO Guest

    Steve May wrote:

    > ChrisO wrote:
    >
    >> Kristofer Pettijohn wrote:
    >>
    >>> I'm defining a hash similiar to what follows:
    >>>
    >>> my $HASH = (
    >>> 'key1' => (
    >>> 'sub1' => 'key1value1',
    >>> 'sub2' => 'key1value2'
    >>> ),
    >>> 'key2' => (
    >>> 'sub1' => 'key2value1',
    >>> 'sub2' => 'key2value2')
    >>> )
    >>>
    >>> and I would like to pass the reference of $HASH{'key1'} to a sub. How
    >>> do I go about doing this?
    >>>

    >>
    >> First of all, define the hash properly using matching braces and not
    >> parens and end with a semicolon:
    >>
    >> my $hash = {
    >> key1 => {
    >> sub1 => 'key1value1',
    >> sub2 => 'key1value2',
    >> },
    >> key2 => {
    >> sub1 => 'key2value1',
    >> sub2 => 'key2value2',
    >> },
    >> };
    >>
    >> The quoting of the keys is not required unless you are going to
    >> include whitespace in your key values.
    >>

    >
    > Ah.... that is not totally correct. When warnings are on and using
    > strict, a key like this-is-a-key will *not* compile.
    >


    I'm not surprised. When I said "whitespace", I had some faint
    recollection of dashes or underscores not working as you pointed out.
    But since I never code without "use warnings qw( all );" and "use
    strict;" I don't find much occasion to trip over those kinds of things.
    I see these two pragmas pretty religiously recommended here in c.l.p.m
    and for good reason.

    -ceo
    ChrisO, Aug 9, 2004
    #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:
    478
    red floyd
    Nov 10, 2011
  2. Srijayanth Sridhar
    Replies:
    19
    Views:
    581
    David A. Black
    Jul 2, 2008
  3. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    102
    Steve
    Sep 28, 2003
  4. Replies:
    14
    Views:
    222
    Tomi Häsä
    Jan 10, 2005
  5. Arvin Portlock
    Replies:
    6
    Views:
    127
    Arvin Portlock
    Sep 2, 2005
Loading...

Share This Page