...the problem is when I pass the hash into the array it is always the same memory reference. Therefore when I access the array every hash reference will be the same and print out the same value.

It's not clear to me exactly what you're doing (because, of course, you don't supply a Short, Self Contained, Correct (Compilable), Example), but my guess is that it's somthing like this:

c:\@Work\Perl\monks>perl -wMstrict -le "my @docArray; my %termsFreq; ;; for my $x (0, 4, 8) { %termsFreq = ($x .. $x+3); push @docArray, \%termsFreq; } ;; for my $hashref (@docArray) { for my $k (sort { $a <=> $b } keys %$hashref) { printf qq{$k => $hashref->{$k} }; } print ''; } " 8 => 9 10 => 11 8 => 9 10 => 11 8 => 9 10 => 11
In this example, the content of the  %termsFreq hash is repeatedly changed, but it's always the same hash, i.e., always the same location in memory. The final content of that location is the last set of data you put into it. Note that the  %termsFreq hash is defined outside the loop.

In contrast, consider:

c:\@Work\Perl\monks>perl -wMstrict -le "my @docArray; ;; for my $x (0, 4, 8) { my %termsFreq = ($x .. $x+3); push @docArray, \%termsFreq; } ;; for my $hashref (@docArray) { for my $k (sort { $a <=> $b } keys %$hashref) { printf qq{$k => $hashref->{$k} }; } print ''; } " 0 => 1 2 => 3 4 => 5 6 => 7 8 => 9 10 => 11
In this example, a new hash is created within the loop and initialized on each pass through the loop, and its reference is taken and push-ed to the array. All of these newly-created hashes (and their contents and addresses) are and remain unique.


Give a man a fish:  <%-{-{-{-<


In reply to Re^5: Creating CSV term document matrix from a hash stored in multideminsional array by AnomalousMonk
in thread Creating CSV term document matrix from a hash stored in multideminsional array 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.