Q: hash of arrays

G

g

Hi,

Given a key, I'm trying to access a specific array in a hash of
arrays. I'm not having much luck....


## Following the Perl docs, I populate the hash of arrays this way:
foreach $sonKey ( keys %son ) {
push @{ $sonRev{$sonKey} }, $son{$sonKey};
}


## This, based on the Perl docs, does work
## in accessing all the elements in the hash of arrays:
foreach $sonKey ( keys %sonRev ) {
print "theAnswer: ";
foreach $i ( 0 .. $#{ $sonRev{$sonKey} } ) {
print " $sonKey = $sonRev{$sonKey}[$i]";
}
print "\n";
}

## But the following doesn't work at all -- nothing is printed
$aKey = 13;

foreach $i ( 0 .. $#{ $sonRev{$aKey} } ) {
print "the sonKey for $aKey is $sonRev{$aKey}[$i]\n";
}


What am I doing wrong?

g
 
N

Nick Pinckernell

What data does your original hash %son have in it?
From your code, I don't see you populating the hash
anywhere.

Because you should just be able to get your ary by:

my @ary = @{$sonRev{"$key"}};

_nick
 
T

Tad McClellan

g said:
I'm trying to access a specific array in a hash of
arrays.

What am I doing wrong?


Not providing a short and complete program that we can run,
as suggested in the Posting Guidelines.

--------------------------
#!/usr/bin/perl
use strict;
use warnings;

my %son = ( 13 => 'thirteen' );
my %sonRev;

foreach my $sonKey ( keys %son ) {
push @{ $sonRev{$sonKey} }, $son{$sonKey};
}

my $aKey = 13;
foreach my $i ( 0 .. $#{ $sonRev{$aKey} } ) {
print "the sonKey for $aKey is $sonRev{$aKey}[$i]\n";
}
 
S

simonis

g said:
Hi,

Given a key, I'm trying to access a specific array in a hash of
arrays. I'm not having much luck....

## Following the Perl docs, I populate the hash of arrays this way:
foreach $sonKey ( keys %son ) {
push @{ $sonRev{$sonKey} }, $son{$sonKey};
}

## This, based on the Perl docs, does work
## in accessing all the elements in the hash of arrays:
foreach $sonKey ( keys %sonRev ) {
print "theAnswer: ";
foreach $i ( 0 .. $#{ $sonRev{$sonKey} } ) {
print " $sonKey = $sonRev{$sonKey}[$i]";
}
print "\n";
}

## But the following doesn't work at all -- nothing is printed
$aKey = 13;

foreach $i ( 0 .. $#{ $sonRev{$aKey} } ) {
print "the sonKey for $aKey is $sonRev{$aKey}[$i]\n";
}

What am I doing wrong?

I whipped together a sample program and can't duplicate a problem,
what specifically does your program output? Any errors? Are you
using warnings? Can you include the actual program, not just a
snippet? Can you also include a small data sample?

Also, if this works:
foreach $sonKey ( keys %sonRev ) {
print "theAnswer: ";
foreach $i ( 0 .. $#{ $sonRev{$sonKey} } ) {
print " $sonKey = $sonRev{$sonKey}[$i]";
}
print "\n";
}

does this work?

#foreach $sonKey ( keys %sonRev ) {
$sonKey = 13;
print "theAnswer: ";
foreach $i ( 0 .. $#{ $sonRev{$sonKey} } ) {
print " $sonKey = $sonRev{$sonKey}[$i]";
}
print "\n";
}


??
 
G

Garry Short

g said:
Hi,

Given a key, I'm trying to access a specific array in a hash of
arrays. I'm not having much luck....


## Following the Perl docs, I populate the hash of arrays this way:
foreach $sonKey ( keys %son ) {
push @{ $sonRev{$sonKey} }, $son{$sonKey};
}

Okay, I *must* be missing something, because my first question is:
1. If you're populating a new hash, surely there are no keys for the foreach
statement to work on?
2. Push works on arrays; why are you trying to use it to populate a hash?

Assuming that all your data is in a file, that you have a key followed by
the entire array, all comma-delimited, you'd do something like:

my %sonRev;
open FILE, $some_file or die "Can't open $some_file: $!\n";
while (<FILE>) {
shift;
my ($sonKey, @array) = split /,/;
$sonRev{$sonKey} = [ @array ];
}
close FILE;

This would create a hash of arrays.

## But the following doesn't work at all -- nothing is printed
$aKey = 13;

foreach $i ( 0 .. $#{ $sonRev{$aKey} } ) {
print "the sonKey for $aKey is $sonRev{$aKey}[$i]\n";
}


What am I doing wrong?

g

foreach $sonKey (sort keys %sonRev) {
print "sonKey = $sonKey; Array = @{ $sonRev{$sonKey} }";
}

prints out the whole hash. If your data is :
1,a,b,c,d
2,e,f,g
3,h,i,j,k,l,m,n
4,o,p,q,r
5,s,t
6,u,v,w,x,y,z
:

you'll get

1 = a b c d
2 = e f g
3 = h i j k l m n
4 = o p q r
5 = s t
6 = u v w x y z

HTH,

Garry.

Complete test code as follows:
1 my %sonRev;
2 while (<DATA>) {
3 shift;
4 ($sonKey, @array) = split /,/;
5 $sonRev{$sonKey} = [ @array ];
6 }
7 foreach $sonKey (sort keys %sonRev) {
8 print "$sonKey = @{ $sonRev{$sonKey} }";
9 }
10
11 __DATA__
12 1,a,b,c,d
13 2,e,f,g
14 3,h,i,j,k,l,m,n
15 4,o,p,q,r
16 5,s,t
17 6,u,v,w,x,y,z
 

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

Similar Threads

hash of arrays 1
having trouble with hash of arrays... 12
Dereferencing Hash of Arrays 7
hash of arrays... 3
Hash key types and equality of hash keys 2
Sorting hash of hashes 3
data to hash 1
assignments of arrays 9

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top