
rpaskudniak
User

Jun 16, 2011, 12:17 PM
Post #1 of 3
(2066 views)
|
Hash of hash containing an array
|
Can't Post
|
|
Greetings, Mr. Family. (I am sooo contrite at not coming here for a while! ) Every once in a while something small comes along to humble you. (Today I spent an hour chasing a mysterious behavior, only to find I had failed to chomp my input! ) But on the the real problem: I am unable to do this without using references. I have a hash, %fu, with a couple members, like an array. That is $fu{one} and $fu{two}. However, they each have two members themselves: $fu{one}{count} = 0; $fu{two}{count} = 1; They also have an array element, which I try to initialize with the following schemes:
$fu{one}{dir} = qw(A B C D E F G H I J K); $fu{one}{dir}[0..10] = qw(A B C D E F G H I J K); $fu{one}{dir}->[0..10] = qw(A B C D E F G H I J K); All the above get syntax errors. Interestingly enough, this works just fine: $fu{one}{dir}[0] = "A"; Similarly, I am trying to use the push and join functions:
push ($fu{key}{dir}, "Z"); Of course that gets a syntax error - it does not look like an array for the push. On the other hand, this gets no syntax error:
my $ar_ref = \$fu{key}{dir}; push (@$ar_ref, "Z3"); but when it runs, I getNot an ARRAY reference at ... I have similar syntax errors with join but I'll wait for this solution; that one will probably be identical. Obviously I am screwing up the syntax; please show me the correct syntax for this kind of assignment. (Something I'm sure I have done before! ) Here's the complete code, with stuff commented out so it would run, that demonstrates what I am trying to do:#!/usr/bin/perl -w -d # use strict; my %fu; $fu{one}{count} = 0; $fu{one}{dir}[0] = "A"; #$fu{one}{dir}->[0..10] = qw(A B C D E F G H I J K); #$fu{two}{count} = 1; #$fu{two}{dir}[0..9] = qw(L M N O P Q R S T U); my $key = "one"; # Just so I can use a variable syntax #push ($fu{key}{dir}, "Z"); #push (@(\($fu{key}{dir})), "Z2"); my $ar_ref = \$fu{key}{dir}[0]; push (@$ar_ref, "Z3"); my $one_str; #$one_str = join("-", $fu{key}{dir}); ##$one_str = join("=", @(\$fu{key}{dir})); $one_str = join("|", @$ar_ref); exit(0); Thanks mucho! -------------------- -- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)
|