CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Hash of hash containing an array

 



rpaskudniak
User


Jun 16, 2011, 12:17 PM

Post #1 of 3 (1734 views)
Hash of hash containing an array Can't Post

Greetings, Mr. Family. (I am sooo contrite at not coming here for a while! Unsure)

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! Blush) 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:


Code
  

$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:


Code
  

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:


Code
my $ar_ref = \$fu{key}{dir}; 
push (@$ar_ref, "Z3");

but when it runs, I get

Code
Not 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! Crazy)

Here's the complete code, with stuff commented out so it would run, that demonstrates what I am trying to do:

Code
#!/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)


miller
User

Jun 16, 2011, 12:26 PM

Post #2 of 3 (1733 views)
Re: [rpaskudniak] Hash of hash containing an array [In reply to] Can't Post

Here are two ways to initialize that array:


Code
$fu{one}{dir} = [qw(A B C D E F G H I J K)];


Or


Code
@{$fu{one}{dir}} = qw(A B C D E F G H I J K);


In either case, you are dealing with a reference.

Just look at perldsc - Hashes of Arrays for more examples of how to work with such structures.

- Miller


rpaskudniak
User


Jun 17, 2011, 12:47 PM

Post #3 of 3 (1715 views)
Re: [miller] Hash of hash containing an array [In reply to] Can't Post

Thanks exteremely, miller.

And yes, I have done it before only I couldn't find the code. So the @ prefix was the solution after all but my error was enclosing the expression with parentheses when I should have been using {braces}. Maybe by repeating it back here, I will remember it for next time.

An amusing afterthought: In my application, the plan was to build the array in a loop and then join all the array elements into a string to be used as the argument list to a shell command. I realized it was less work overall to simply build the argument list in the loop by contenating each newly read item (with a blank separator) to the string. At the loop's exit, the arument list is already built and ready to be used. Simplicty trumping the PERLy gates. Angelic

Sometimes, ya gotta travel 'round the world to end up marrying the girl next door. Cool
--------------------
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives