J
Jennifer I. Drake
Hello,
I created a hash of arrays and I'm having problems dereferencing the
array and printing the array out. In one subroutine, I create the hash
and put the data into it. From that subroutine, I call another
subroutine and pass the reference to the hash. In the second
subroutine, I sort the hash and try to dereference the arrays. But,
when I try to print the arrays, nothing appears. I know that there is
data in the original array given to the hash and I know that the hash
keys are valid. Any help would be appreciated with where I've gone
wrong.
Thanks!
-Jen
#!/usr/bin/perl
use strict;
use warnings;
my %keys;
&GetData(\%keys);
###subroutine 1 - GetData###
### Retrieving information from a PostgreSQL database here.
my %data;
my $keysRef;
foreach my $secondKey (@{$secondKeysRef}) {
while (($key, @values) = $sth->fetchrow_array) {
$$keysRef{$key} = 1;
$data{$key}{$secondKey} = \@values;
}
&StoreData(\%data, $keysRef);
}
###subroutine 2 - StoreData###
my ($dataRef, $keysRef) = @_;
my $outputfile = "Output.txt";
open (OUT, ">$outputfile") or die "Cannot make $outputfile: $!\n";
foreach my $key (sort keys %{$dataRef}) { ##loop through each of the
first key
print OUT $key;
for (my $i = 0; $i<3; $i++) { ##loop through each of the second
keys - hardcoded for now, but will be a variables to control count
later
my $valuesRef = $$dataRef{$key}{$$keysRef[$i]};
print OUT "\t";
print OUT join ("\t", @$valuesRef);
}
print OUT "\n";
}
I created a hash of arrays and I'm having problems dereferencing the
array and printing the array out. In one subroutine, I create the hash
and put the data into it. From that subroutine, I call another
subroutine and pass the reference to the hash. In the second
subroutine, I sort the hash and try to dereference the arrays. But,
when I try to print the arrays, nothing appears. I know that there is
data in the original array given to the hash and I know that the hash
keys are valid. Any help would be appreciated with where I've gone
wrong.
Thanks!
-Jen
#!/usr/bin/perl
use strict;
use warnings;
my %keys;
&GetData(\%keys);
###subroutine 1 - GetData###
### Retrieving information from a PostgreSQL database here.
my %data;
my $keysRef;
foreach my $secondKey (@{$secondKeysRef}) {
while (($key, @values) = $sth->fetchrow_array) {
$$keysRef{$key} = 1;
$data{$key}{$secondKey} = \@values;
}
&StoreData(\%data, $keysRef);
}
###subroutine 2 - StoreData###
my ($dataRef, $keysRef) = @_;
my $outputfile = "Output.txt";
open (OUT, ">$outputfile") or die "Cannot make $outputfile: $!\n";
foreach my $key (sort keys %{$dataRef}) { ##loop through each of the
first key
print OUT $key;
for (my $i = 0; $i<3; $i++) { ##loop through each of the second
keys - hardcoded for now, but will be a variables to control count
later
my $valuesRef = $$dataRef{$key}{$$keysRef[$i]};
print OUT "\t";
print OUT join ("\t", @$valuesRef);
}
print OUT "\n";
}