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