strange hash syntax

Discussion in 'Perl Misc' started by Torch, Nov 14, 2003.

  1. Torch

    Torch Guest

    I encountered a hash syntax I have never seen before, can somebody
    help me understanding it.

    $somehash{$somekey} = [ @somearray];

    When I do print"$somehash{$somekey}" I get some strange number
    ARRAY(0x20038114)as an output.
    What does this mean????
    I can;t find anything in my book and I do not nkow how to start
    looking for it on the net...
    Thx for any help!

    Torch
     
    Torch, Nov 14, 2003
    #1
    1. Advertising

  2. In article <>, Torch wrote:
    > I encountered a hash syntax I have never seen before, can somebody
    > help me understanding it.
    >
    > $somehash{$somekey} = [ @somearray];



    This stores an array reference at the given location of the
    hash. The %somehash now contains an array reference at the key
    $somekey. You can index it like this:

    # Third element of the stored array:
    my $e = $somehash{$somekey}[2];

    Or loop over it:

    foreach my $e (@{ $somehash{$somekey} }) {
    # Make use of array element $e here
    }


    Read the perldata manual.


    --
    Andreas Kähäri
     
    Andreas Kahari, Nov 14, 2003
    #2
    1. Advertising

  3. (Torch) writes:

    > $somehash{$somekey} = [ @somearray];


    > What does this mean????
    > I can;t find anything in my book


    Others have addressed your immediate problem but if your book doesn't
    explain [...] it sounds like it is probably a Perl4 book. There's a
    lot of stuff in Perl5 that's not in Perl4 and there's a lot of stuff
    that you had to do in Perl4 to work around its shortcommings that's
    now considered very poor style in Perl5.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Nov 14, 2003
    #3
  4. Torch

    Dave Oswald Guest

    "Torch" <> wrote

    > I encountered a hash syntax I have never seen before, can somebody
    > help me understanding it.
    >
    > $somehash{$somekey} = [ @somearray];
    >
    > When I do print"$somehash{$somekey}" I get some strange number
    > ARRAY(0x20038114)as an output.
    > What does this mean????
    > I can;t find anything in my book and I do not nkow how to start
    > looking for it on the net...


    You're looking at the creation of an anonymous array, whos referent will be
    a hash element.

    If your book doesn't discuss references and anonymous arrays, you'd better
    get a different book, or read 'perlref' from the Perl POD.
     
    Dave Oswald, Nov 15, 2003
    #4
  5. Darek N <> wrote:

    >> # Third element of the stored array:
    >> my $e = $somehash{$somekey}[2]; ## need to derefernce !!

    >
    > This is not quite right



    Yes it is.


    > because of the dereferencing action:
    > my $e = $somehash{$somekey}->[2];



    perldoc perlref

    One more thing here. The arrow is optional _between_
    brackets subscripts...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 23, 2003
    #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. Red Orchid
    Replies:
    3
    Views:
    1,048
  2. Harvey Twyman
    Replies:
    8
    Views:
    569
    August Derleth
    Oct 25, 2003
  3. Pieter Claassen
    Replies:
    1
    Views:
    1,116
    CBFalconer
    Aug 4, 2004
  4. rp
    Replies:
    1
    Views:
    538
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
Loading...

Share This Page