Multi-dim hash slices

Discussion in 'Perl Misc' started by Dinko Korunic, Jan 17, 2005.

  1. Hi. Is it possible to use hash slices with multi-dimensional hashes (and
    how)? If yes, would hash slices be in any way faster for code that
    basically does:

    $hash{$array_elem1}{$array_elem2} = 1;

    for each element of each array?

    TIA.

    --
    | |--.----.-----. Dinko 'kreator' Korunic #include <stddisclaimer.h>
    | <| _| -__| http://kreator.esa.fer.hr/ | http://kre.deviantart.com/
    |__|__|__| |_____| PGP:0xEA160D0B | IRC:kre | ICQ:16965294 | AIM:kreatorMoo
     
    Dinko Korunic, Jan 17, 2005
    #1
    1. Advertising

  2. Dinko Korunic wrote:
    > Is it possible to use hash slices with multi-dimensional hashes (and
    > how)?


    Yes. Example:

    my %hash;
    my @keys = qw/foo bar baz/;
    @{ $hash{abc} }{ @keys } = (1) x @keys;

    > If yes, would hash slices be in any way faster for code that
    > basically does:
    >
    > $hash{$array_elem1}{$array_elem2} = 1;
    >
    > for each element of each array?


    If speed is important for you (or if you are just curious), do a
    benchmark to find out.

    perldoc Benchmark

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 17, 2005
    #2
    1. Advertising

  3. Dinko Korunic <> wrote:

    > Is it possible to use hash slices with multi-dimensional hashes



    Yes.


    > (and
    > how)?



    The way it says to in perlreftut.pod.

    I like to apply "Use Rule 1" in 3 steps:

    @slice_me{ 'foo', 'bar' } # pretend it is a plain hash

    @{ }{ 'foo', 'bar' } # replace the name with a block ...

    @{ $inner{baz} }{ 'foo', 'bar' } # ... that returns the
    # proper kind of reference

    > If yes, would hash slices be in any way faster



    use Benchmark;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 18, 2005
    #3
    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. Denis Palas
    Replies:
    1
    Views:
    528
  2. Florian Kaufmann
    Replies:
    1
    Views:
    390
  3. Jack

    Allocating multi-dim'l arrays

    Jack, Jul 12, 2011, in forum: C Programming
    Replies:
    7
    Views:
    443
    Keith Thompson
    Jul 13, 2011
  4. rp
    Replies:
    1
    Views:
    581
    red floyd
    Nov 10, 2011
  5. Gijs Nijholt

    multi dim array?

    Gijs Nijholt, Oct 20, 2006, in forum: Ruby
    Replies:
    4
    Views:
    118
    Robert Klemme
    Oct 20, 2006
Loading...

Share This Page