lobs has asked for the wisdom of the Perl Monks concerning the following question:
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: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);
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):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}; } { } }
Then add the hash into an array of documents as follows:while ($element = shift(@numOfWordArr)) { $termsFreq{$element} ++; if(!exists($docTerms{$element})) { $docTerms{$element}++; } }
Finally pass the array into an array to seperate the classes of the documents:push(@docArray, \%termsFreq);
took the advice from this thread and did as follows to print the doc matrix to a csv file:push(@classArr, \@docArray);
As stated above I get rows of the same value since it is accessing the same hash per iteration. Help is much appreciated. Thanks!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"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Get hash values in multidimensional array
by BillKSmith (Monsignor) on Feb 24, 2017 at 20:43 UTC | |
|
Re: Get hash values in multidimensional array
by poj (Abbot) on Feb 24, 2017 at 20:25 UTC | |
|
Re: Get hash values in multidimensional array
by AnomalousMonk (Archbishop) on Feb 25, 2017 at 01:12 UTC | |
by Anonymous Monk on Mar 01, 2017 at 21:28 UTC | |
|
Re: Get hash values in multidimensional array
by 1nickt (Canon) on Feb 25, 2017 at 02:11 UTC | |
|
Re: Get hash values in multidimensional array
by Marshall (Canon) on Feb 25, 2017 at 01:32 UTC | |
|
Re: Get hash values in multidimensional array
by Laurent_R (Canon) on Feb 24, 2017 at 20:15 UTC | |
by toolic (Bishop) on Feb 24, 2017 at 20:23 UTC |