Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Creating CSV term document matrix from a hash stored in multideminsional array

by stevieb (Canon)
on Mar 03, 2017 at 20:06 UTC ( [id://1183596]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Creating CSV term document matrix from a hash stored in multideminsional array
in thread Creating CSV term document matrix from a hash stored in multideminsional array

Did that work?

Note I updated my original reply before you posted this update, but it would be more idiomatic Perl to rewrite it something like this (untested):

for my $class (@classArr){ for my $href (@$class){ for my $key (sort keys %$href){ print $csv "$key-- $href->{$key},"; } } }
  • Comment on Re^3: Creating CSV term document matrix from a hash stored in multideminsional array
  • Download Code

Replies are listed 'Best First'.
Re^4: Creating CSV term document matrix from a hash stored in multideminsional array
by lobs (Acolyte) on Mar 04, 2017 at 01:21 UTC
    No that didn't work I know 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.
      ...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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1183596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found