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