Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Q: hash of arrays

Reply
Thread Tools

Q: hash of arrays

 
 
g
Guest
Posts: n/a
 
      08-14-2003
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
 
Reply With Quote
 
 
 
 
Nick Pinckernell
Guest
Posts: n/a
 
      08-14-2003

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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      08-14-2003
g <(E-Mail Removed)> wrote:

> 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";
}
--------------------------


Works fine for me...


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
simonis
Guest
Posts: n/a
 
      08-14-2003
g wrote:
>
> 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";
}


??
 
Reply With Quote
 
Garry Short
Guest
Posts: n/a
 
      08-14-2003
g wrote:

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

<SNIP>
>
> ## 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash of Hash of Arrays Question Älphä Blüë Ruby 5 07-18-2009 07:36 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
hash of arrays - appending to one of the arrays Adam Akhtar Ruby 5 03-25-2008 11:53 PM
Sort a hash based on values in the hash stored as arrays of hashes Tore Aursand Perl Misc 3 09-16-2003 10:14 AM



Advertisments