I have an issue with accessing hash values within 2 arrays. The way the hash is stored in the arrays is as follows:
my @arr;
my @subArr;
my %hash = ();
my %hash2 = ();
$hash{key} = 12;
$hash{pl} = 1;
$hash{lop} = 9;
$hash2{key} = 12;
$hash4{pl} = 1;
$hash8{lo} = 9;
push(@subArr, \%hash);
push(@subArr, \%hash2);
push(@arr, \@subArr);
I'm not sure how to get the value of each key in the hashes. I keep on getting the reference to memory or 1/8. This is my code:
for my $i (0 .. $#arr){
$subd = $arr[i];
print"subLen: ".$subd."\n";
for my $j ( {{$arr[i]}} )
{
foreach my $key{ keys %{$j}}{
print $key.": ".${$j}}{$key};
}
{
}
}
EDIT:
The solution in the comments worked but I tried to implement it in my program that is trying to get a term document matrix happens to get only one hash and print out values for that hash.
I get the hash function by going through an array and adding the terms to the hash as follows (hash %termFreq is specific for the document and hash %docTerms is the features of all documents):
while ($element = shift(@numOfWordArr)) {
$termsFreq{$element} ++;
if(!exists($docTerms{$element}))
{
$docTerms{$element}++;
}
}
Then add the hash into an array of documents as follows:
push(@docArray, \%termsFreq);
Finally pass the array into an array to seperate the classes of the documents:
push(@classArr, \@docArray);
took the advice from this thread and did as follows to print the doc matrix to a csv file:
for my $i($#classArr) {
print "subArr_ref: ".@{$classArr[$i]}."\n";
for my $hashRef(@{$classArr[$i]}) {
print "hashRef: ".$hashRef."\n";
for my $key (sort keys %$hashRef) {
print $csv $key."-- ".$hashRef->{$key}.",";
}
# foreach my $feat(@featureVector) {
# print $csv $hashRef->{$feat}.",";
# }
print $csv $i."\n";
}
}
As stated above I get rows of the same value since it is accessing the same hash per iteration.
Help is much appreciated.
Thanks!