Hashes of Hashes via subs

B

Ben Holness

Hi All,

I want to create a hash array, based on values in a database. Basically, I
want a hash array for each database key and I want to use a sub to get the
hash array, but I am having a great deal of difficulty!!

I have written an example script, taking out the DB side of things, to
explain what I want to do and how I want to do it. I am obviously doing
something wrong, but I don't know what :)

The end result (i.e. what is printed) needs to be:

Looking at 1 : a. Name is array1a
Looking at 1 : b. Name is array1b
Looking at 2 : a. Name is array2a
Looking at 2 : b. Name is array2b

--

Looking at 1 : a. Name is array1a
Looking at 1 : b. Name is array1b
Looking at 2 : a. Name is array2a
Looking at 2 : b. Name is array2b

but I only get the first set of printouts with the script as it is :(

In this example, I have kept the foreach statement the same in both ways
of doing it. I have tried accessing the hashes in a number of different
ways, but without success.

If anyone can point out what I am doing wrong, I would greatly appreciate
it.

Although I could re-write the actual code to work in a different way, I
would prefer not to. Ideally, I would be able to get it working like I
have laid out in this script.

Many thanks,

Ben

--

#!/usr/bin/perl

# This is what I want:

my %array1;
$array1{"1"}{"a"}{"Name"}="array1a";
$array1{"1"}{"a"}{"Value"}="value1a";
$array1{"1"}{"b"}{"Name"}="array1b";
$array1{"1"}{"b"}{"Value"}="value1b";

$array1{"2"}{"a"}{"Name"}="array2a";
$array1{"2"}{"a"}{"Value"}="value2a";
$array1{"2"}{"b"}{"Name"}="array2b";
$array1{"2"}{"b"}{"Value"}="value2b";

foreach my $level1 (keys %array1)
{
foreach my $level2 (keys %{$array1{$level1}})
{
print "Looking at $level1 : $level2. Name is ".$array1{$level1}{$level2}{"Name"}."\n";
}
}

print "\n--\n\n";

# But I want to do it like this

my %array2;

$array2{"1"}=getSubArrays("1");
$array2{"2"}=getSubArrays("2");

foreach my $level1 (keys %array2)
{
foreach my $level2 (keys %{$array2{$level1}})
{
print "Looking at $level1 : $level2. Name is ".$array2{$level1}{$level2}{"Name"}."\n";
}
}


sub getSubArrays
{
my %tempArray;
$tempArray{"a"}{"Name"}="array".$_[0]."a";
$tempArray{"b"}{"Name"}="array".$_[0]."b";
return %tempArray;
}
 
B

Ben Holness

Just noticed a small error in the getSubArrays subroutine - I forgot to
put in the values :)

sub getSubArrays
{
my %tempArray;
$tempArray{"a"}{"Name"}="array".$_[0]."a";
$tempArray{"a"}{"Value"}="value".$_[0]."a";
$tempArray{"b"}{"Name"}="array".$_[0]."b";
$tempArray{"b"}{"Value"}="value".$_[0]."b";
return %tempArray;
}

Cheers,

Ben
 
B

Ben Holness

Worked out how to it :)

It's as simple as curly brackets around the function calls to
getSubArrays!!

Thanks for looking,

Ben
 
R

Roy Johnson

It is recommended that you post questions to comp.lang.perl.misc. This
newsgroup is technically defunct.
 
N

nobull

In response to his own FAQ "Ben Holness said:
Worked out how to it :)
TMTOWTDI!

It's as simple as curly brackets around the function calls to
getSubArrays!!
$array2{"1"}={getSubArrays("1")};

sub getSubArrays
{
my %tempArray; # [snip!]
return %tempArray;
}

Yes, but it is not efficient. See FAQ: "How can I [...] return a [...]
Hash [...]?"

Oh, and please refrain from TOFU, it is considered rude - even when
following up yourself.

Also, it is confusing to alter quoted material without making it clear
you are doing so - even when quoting youself.

Others have pointed out this newsgroup does not exist. Please do not
start threads here.
 
B

Ben Holness

TMTOWTDI!

Please can someone decipher for me? :)
Yes, but it is not efficient. See FAQ: "How can I [...] return a [...]
Hash [...]?"

Thanks - I'll have a look
Oh, and please refrain from TOFU, it is considered rude - even when
following up yourself.

TOFU = Typing Own Follow Up?
Sorry - just wanted to (a) let any other interested parties know the
answer (even if it turned out not to be the best one, but they would know
that too now :) ) and (b) it means that other people know the problem has
been solved and don't need to waste their time looking at it.
Also, it is confusing to alter quoted material without making it clear
you are doing so - even when quoting youself.

I don't usually, it's just that the sub had changed so it shouldn't be all
quoted, and it didn't look right with only some of it quoted.
Others have pointed out this newsgroup does not exist. Please do not
start threads here.

This was my first post in this newsgroup. I was not aware of the status of
it but I am now, so I wont start any new threads here :)

Apologies to anyone else that I have offended. Lessons learnt!

Cheers

Ben
 
N

nobull

Ben Holness said:
Please can someone decipher for me? :)

There's more than one way to do it. (A motto of the Perl community).

(Actually maybe it's written TIMTOWTDI).
TOFU = Typing Own Follow Up?

[new] Text Over, Full-quote Under. Quoted material should be
interspersed with new material so as to give context the the new
matierial that follows it. Only material needed to give context
should be included.
I don't usually, it's just that the sub had changed so it shouldn't be all
quoted, and it didn't look right with only some of it quoted.

I sympathise but it did cause me confusion. You appeared to be saying
you'd found a solution to the quoted problem - but the quoted material
didn't illustrate the problem! Better to simply unquote the lot
IMNSHO.
This was my first post in this newsgroup. I was not aware of the status of
it but I am now, so I wont start any new threads here :)

Curious - did you look at any of the threads before you posted? In
most the status of the newsgroup is mentioned.
Apologies to anyone else that I have offended.

I think "offended" would be too strong a word.
Lessons learnt!

That's what it's all about.
 
R

Roy Johnson

Ben Holness said:
Please can someone decipher for me? :)

There's More Than One Way To Do It. (the Perl motto)
TOFU = Typing Own Follow Up?

Text over, fullquote under. That is, replying at the top, and
including the entire original post below the reply.
 
B

Ben Holness

Curious - did you look at any of the threads before you posted? In
most the status of the newsgroup is mentioned.

No - I searched on Google for other people asking the same question and I
skim read the headers, but given that there 300 odd posts in the last
month, it never occurred to me that this newsgroup was not supposed to be
used :)

Cheers,

Ben
 

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


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top