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!

In reply to Get hash values in multidimensional array to populate Term document matrix by lobs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.