473,657 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"}="arr ay1a";
$array1{"1"}{"a "}{"Value"}="va lue1a";
$array1{"1"}{"b "}{"Name"}="arr ay1b";
$array1{"1"}{"b "}{"Value"}="va lue1b";

$array1{"2"}{"a "}{"Name"}="arr ay2a";
$array1{"2"}{"a "}{"Value"}="va lue2a";
$array1{"2"}{"b "}{"Name"}="arr ay2b";
$array1{"2"}{"b "}{"Value"}="va lue2b";

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

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

# But I want to do it like this

my %array2;

$array2{"1"}=ge tSubArrays("1") ;
$array2{"2"}=ge tSubArrays("2") ;

foreach my $level1 (keys %array2)
{
foreach my $level2 (keys %{$array2{$leve l1}})
{
print "Looking at $level1 : $level2. Name is ".$array2{$leve l1}{$level2}{"N ame"}."\n";
}
}
sub getSubArrays
{
my %tempArray;
$tempArray{"a"} {"Name"}="array ".$_[0]."a";
$tempArray{"b"} {"Name"}="array ".$_[0]."b";
return %tempArray;
}
Jul 19 '05 #1
8 2743
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"}="valu e".$_[0]."a";
$tempArray{"b"} {"Name"}="array ".$_[0]."b";
$tempArray{"b"} {"Value"}="valu e".$_[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"}={g etSubArrays("1" )};
$array2{"2"}={g etSubArrays("2" )};

foreach my $level1 (keys %array2)
{
foreach my $level2 (keys %{$array2{$leve l1}})
{
print "Looking at $level1 : $level2. Name is ".$array2{$leve l1}{$level2}{"N ame"}."\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"}={g etSubArrays("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
2288
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 the Sub Application_OnEnd is executed, but is does not say that anything else is executed Actually I even can't understand if global.asa is permited to have any code outside the Subs
13
2089
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 with the code. So I have decided to try and make things neater by grouping it into subs. So i have a sub to create the variables, a sub for the connection etc. Only problem is when i call my subs, it says that the variable is undefined. If i...
2
1493
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() datagrid.datasource=ds.tables("t").defautview datagrid.databind() end sub
7
1867
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 want to call one sub that will utilize a database connection from within another sub that is already utilizing a database connection. 1) Can I just declare this database connection once outside of all subroutines to make it global (the way I...
11
1862
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 mean, for one, delegates point to subs, so when you call a delegate, why not just call the sub dierectly and not bother adding the extra code involved adding the delegate?
5
3143
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 same class. Is this correct? It would be nice to declare the MySelf instance in the Class public space (just under Private Class Class1). That will give this error: Cannot refer to an instance member of a class from within a shared
2
2849
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 way to get this hash value, and be able to verify it against user logins in both Classic ASP and ASP.NET? We have been working with the system.security.cryptopgraphy MD5 functions in .NET, and the functions here for Classic ASP...
0
1323
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 file and a cdf file I need also to keep the data structure For this I chose the following strategy: -dumping the files into two arrays -doing a pattern matching between the two files.
6
9607
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 serialization The code itself generates a list of strings comprised of random numbers. No number will be repeated within a string, and no string will be repeated within the list of strings. Following the code is a brief discussion of how the above...
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.