473,395 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Hashes of Hashes via subs

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;
}
Jul 19 '05 #1
8 2731
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
Jul 19 '05 #2
Worked out how to it :)

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

Thanks for looking,

Ben
# 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;
}


Jul 19 '05 #3
It is recommended that you post questions to comp.lang.perl.misc. This
newsgroup is technically defunct.
Jul 19 '05 #4
In response to his own FAQ "Ben Holness" <us****@bens-house.org.uk> wrote in message news:<pa****************************@bens-house.org.uk>...
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.
Jul 19 '05 #5
> 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
Jul 19 '05 #6
"Ben Holness" <us****@bens-house.org.uk> wrote in message news:<pa****************************@bens-house.org.uk>...
TMTOWTDI!
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).
Oh, and please refrain from TOFU 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.
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.


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


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.
Jul 19 '05 #7
"Ben Holness" <us****@bens-house.org.uk> wrote in message news:<pa****************************@bens-house.org.uk>...
TMTOWTDI!
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.
Jul 19 '05 #8
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.


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
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: | last post by:
In global.asa I have some code outside all the Subs. I have some confirmation that it is being executed OnStart. Yet I can's see if it is executed OnEnd. The literature I have says that OnEnd...
13
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in...
2
by: nakhi | last post by:
Hi, I declared a dataset outside any subs, then I fill the dataset with a tables in a Private Sub1, then I want to take the data out from the dataset in Sub2. like . sub2()...
7
by: djc | last post by:
I have several subroutines (all inline code) that wind up using the same database connection object variable. I have been declaring a new variable in every sub. I just now came to a point where I...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
5
by: Brett | last post by:
In a class, I have several Private subs. I declare an instance of the class such as: Dim MySelf as new Class1 within a private sub. The motive is to provide access to other subs within the...
2
by: MartyNg | last post by:
I am running a system that has both Classic ASP applications and a smattering of ASP.NET applications. We want to store passwords on a SQL Server table as their MD5 hashes. What is the safest...
0
by: HalfCoded | last post by:
hi everyone, I am kind of stuck and therefore would really appreciate some clues: I actually have to run a script which has to compare two elements from two different files which are a blast...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.