Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by lobs (Acolyte)
on Mar 03, 2017 at 20:02 UTC ( [id://1183595] : note . print w/replies, xml ) Need Help??


in reply to Re: 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

yea I changed it recently and its now:
foreach my $subArr_ref(@classArr) { print "subArr_ref: ".@{$subArr_ref}."\n"; foreach my $hashRef(@{$subArr_ref}) { print "hashRef: ".$hashRef."\n"; foreach my $key (sort keys %{$hashRef}) { #print $csv $key.":".$hashRef->{$key}.","; print $csv "$key : ${$hashRef}{$key},"; } # foreach my $feat(@featureVector) { # print $csv $hashRef->{$feat}.","; # } print $csv $i."\n"; } }
  • Comment on Re^2: Creating CSV term document matrix from a hash stored in multideminsional array
  • Download Code

Replies are listed 'Best First'.
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

    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},"; } } }
      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:  <%-{-{-{-<