Hash reference question

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

  1. I'm defining a hash as 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 wrote:
    > I'm defining a hash as follows:
    >
    > my $HASH = (
    > 'key1' => (
    > 'sub1' => 'key1value1',
    > 'sub2' => 'key1value2'
    > ),
    > 'key2' => (
    > 'sub1' => 'key2value1',
    > 'sub2' => 'key2value2')
    > )


    Please "use warnings;"! If you had done so, you'd understand that you
    should use braces instead of parentheses.

    > and I would like to pass the reference of $HASH{'key1'} to a sub.


    Since there is no %HASH, there is no $HASH{'key1'} either. Maybe you
    mean $$HASH{key1} or $HASH->{key1}.

    > How do I go about doing this?


    mysub( $HASH->{key1} );

    You have some reading to do. Start here:

    perldoc perlreftut
    perldoc perldsc

    Good luck!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 8, 2004
    #2
    1. Advertising

  3. Kristofer Pettijohn

    Guest

    (Kristofer Pettijohn) wrote in message news:<4115a05e$0$2534$>...
    > I'm defining a hash as follows:
    >
    > my $HASH = (
    > 'key1' => (
    > 'sub1' => 'key1value1',
    > 'sub2' => 'key1value2'
    > ),
    > 'key2' => (
    > 'sub1' => 'key2value1',
    > 'sub2' => 'key2value2')
    > )


    That is a eqivalent to

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

    Somehow I think you meant to say:

    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.


    Do you mean the reference that is stored _in_ $HASH{key1} or a
    reference _to_ the scalar storage location $HASH{key1} (i.e.
    \$HASH{key1} ) ?

    > How do I go about doing this?


    In either case the answer is - just do it.

    This newsgroup does not exist (see FAQ). Please do not start threads
    here.
     
    , Aug 8, 2004
    #3
  4. Kristofer Pettijohn

    Guest

    wrote in message news:<>...
    > (Kristofer Pettijohn) wrote in message news:<4115a05e$0$2534$>...
    > > I'm defining a hash as follows:
    > >
    > > my $HASH = (
    > > 'key1' => (
    > > 'sub1' => 'key1value1',
    > > 'sub2' => 'key1value2'
    > > ),
    > > 'key2' => (
    > > 'sub1' => 'key2value1',
    > > 'sub2' => 'key2value2')
    > > )

    >
    > That is a eqivalent to
    >
    > my $HASH = (
    > 'key1' => 'sub1',
    > 'key1value1' => 'sub2',
    > 'key1value2' => 'key2',
    > 'sub1' => 'key2value1',
    > 'sub2' => 'key2value2',
    > )


    Or just

    my $HASH = 'key2value2';

    Sorry I read $HASH as %HASH. Funny thing, the power of suggestion.

    > Somehow I think you meant to say:
    >

    [ snip gibberish ]
    my $HASH = {
    'key1' => {
    'sub1' => 'key1value1',
    'sub2' => 'key1value2',
    },
    'key2' => {
    'sub1' => 'key2value1',
    'sub2' => 'key2value2'.
    },
    };

    or

    my %HASH = (
    'key1' => {
    'sub1' => 'key1value1',
    'sub2' => 'key1value2',
    },
    'key2' => {
    'sub1' => 'key2value1',
    'sub2' => 'key2value2'.
    },
    );

    > This newsgroup does not exist


    So at least I can take comfort from the fact that few people will see
    this bit of idiocy.
     
    , Aug 9, 2004
    #4
    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:
    537
    red floyd
    Nov 10, 2011
  2. Srijayanth Sridhar
    Replies:
    19
    Views:
    625
    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:
    120
    Steve
    Sep 28, 2003
  4. Replies:
    14
    Views:
    250
    Tomi Häsä
    Jan 10, 2005
  5. Arvin Portlock
    Replies:
    6
    Views:
    141
    Arvin Portlock
    Sep 2, 2005
Loading...

Share This Page