Multi-dim hash slices

D

Dinko Korunic

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.
 
G

Gunnar Hjalmarsson

Dinko said:
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
 
T

Tad McClellan

Dinko Korunic said:
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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top