Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: hash pointing to AoA

by mwah (Hermit)
on May 19, 2008 at 13:29 UTC ( #687382=note: print w/replies, xml ) Need Help??


in reply to hash pointing to AoA

An additional (interesting?) question would be: how can I create an AoA on a hash key 'in place', for example I want to create a 10x10 boolean array that is located in a hash under a specific key, e.g.:

# example: build a 10x10 boolean field on a hash key my $key='10x10'; my %hash; for my $row (0..9) { for my $col (0..9) { push @{ $hash{$key}[$row] }, int(rand()+0.5); } }

Afterwards, we could run through our array by iteration over indexes:

for my $row (0..9) { for my $col (0..9) { print "{$key} => [$row][$col] = $hash{$key}[$row][$col] \n" } }

Regards

mwa

Replies are listed 'Best First'.
Re^2: hash pointing to AoA
by NetWallah (Canon) on May 19, 2008 at 16:35 UTC
    Here is a somewhat more idiomatic implementation of your code:
    use strict; my %hash; my $key='NetWallah'; $hash{$key} = [ map{ [ map {int(rand()+0.5)} 0..9 ] } 0..9 ]; printit(\%hash); sub printit{ my $href=shift; for my $key(sort keys %$href){ my $row = 0; print "{$key} =>\n"; for my $rowref (@{$href->{$key}}) { print " $row\t"; for my $val (@$rowref) { print "$val "; } $row++; print "\n"; } } }

         "How many times do I have to tell you again and again .. not to be repetitive?"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://687382]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2021-04-10 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?