Q: hash of arrays

Discussion in 'Perl Misc' started by g, Aug 14, 2003.

  1. g

    g Guest

    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
    g, Aug 14, 2003
    #1
    1. Advertising

  2. 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
    Nick Pinckernell, Aug 14, 2003
    #2
    1. Advertising

  3. g <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Aug 14, 2003
    #3
  4. g

    simonis Guest

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


    ??
    simonis, Aug 14, 2003
    #4
  5. g

    Garry Short Guest

    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
    Garry Short, Aug 14, 2003
    #5
    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. Philipp
    Replies:
    21
    Views:
    1,109
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    499
    red floyd
    Nov 10, 2011
  3. Adam Akhtar
    Replies:
    5
    Views:
    637
    Adam Akhtar
    Mar 25, 2008
  4. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    658
    Älphä Blüë
    Jul 18, 2009
  5. Tore Aursand
    Replies:
    3
    Views:
    549
    Anno Siegel
    Sep 16, 2003
Loading...

Share This Page