http://qs1969.pair.com?node_id=1183589

lobs has asked for the wisdom of the Perl Monks concerning the following question:

PROBLEM AT HAND: every row in my csv file has the same value. I am trying to implement a term document matrix and store it in a CSV file.In my program I store the features and the value within a hash for a document. Each hash is stored in an array. The array then is stored within another array to seperate the document classes. 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 another thread to iterate through the arrays to get a hash value and print the doc matrix to a csv file:
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"; }
My problem is that I get rows of the same value since it is accessing the same hash per iteration. Help is much appreciated. Thanks!