Re: hash of arrays or separate hashes?
by halley (Prior) on Mar 04, 2004 at 15:52 UTC
|
You can only associate one single scalar value with each unique string key. However, thanks to references, which are scalar values, that's not as limiting as that sounds.
%Elementsfreq =
( 'Alpha' => [ 37, [ 0, 0, 4, 26, 0, 7 ] ],
'Beta' => [ 40, [ 1, 38, 0, 0, 0, 1 ] ],
'Gamma' => [ 3, [ 1, 1, 0, 0, 0, 1 ] ] );
You would use $count = $Elementsfreq{Beta}[0] to retrieve that 40, or $count = $Elementsfreq{Alpha}[1][3] to grab that 26. The square brackets produce references to unnamed arrays.
Your pseudocode gave curly braces for your lists of values. Curly braces similarly produce references to unnamed hashes. If you meant them to be hashes, then replace the innermost square brackets in my example.
This is a somewhat advanced topic, but perldoc perllol should get you started with complex data structures.
-- [ e d @ h a l l e y . c c ]
| [reply] [d/l] [select] |
|
|
Thank you Monks!!! :) I am going to try your advices now
| [reply] |
Re: hash of arrays or separate hashes?
by hardburn (Abbot) on Mar 04, 2004 at 15:54 UTC
|
Hash keys always have to be unique, but there might be a way around your problem by restructuring your data. The value could be a hashref containing keys that store the count and the array:
Alpha => {
count => 37,
data => [0,0,4,26,0,7],
},
Beta => {
count => 40,
data => [1,38,0,0,0,1],
},
Gamma => {
count => 3,
data => [1,1,0,0,0,1],
},
Notice that data in each hashref contains an arrayref, which is proably what you want (the orginal you posted would have made a hashref).
----
: () { :|:& };:
Note: All code is untested, unless otherwise stated
| [reply] [d/l] [select] |
Re: hash of arrays or separate hashes?
by broquaint (Abbot) on Mar 04, 2004 at 15:55 UTC
|
Since hash values can only contain a scalar value you can't actually hold two values. Instead, what you can do is store an array of two values e.g
my %hash = (
Alpha => [
37
[0,0,4,26,0,7]
],
Beta => [
40,
[1,38,0,0,0,1],
],
Gamma => [
3
[1,1,0,0,0,1],
],
);
See. perldsc for more information.
| [reply] [d/l] |
|
|
Monks I need all your help again, sorry I new on this..
Before I use to countfreq for an element using
$Elementsfreq{$Element}++;
Now to increment the count, do I have to have another variable?, Could
+ you please tell me what is wrong with the following?:
$countElements++;
%Elementsfreq = ($Element => [$countElements, [ 0..$index ] ]);
$bigramfrequency{$bigram}[0]++;
$bigramfrequency{$bigram}[1][$position]++;
Thanks!!
| [reply] [d/l] |
|
|
oops I am counting twice.. Please disregard the $countElement++. But Still is the rest ok?
| [reply] |
Re: hash of arrays or separate hashes?
by matija (Priest) on Mar 04, 2004 at 15:54 UTC
|
You can have a hash of hashes, like this:
Alpha
count => 37
arr => {0,0,4,26,0,7}
Beta
count => 40
arr => {1,38,0,0,0,1}
Gamma
count => 3
arr => {1,1,0,0,0,1}
And you assing it like this:$hash{Alpha}={count=>37, arr=>[0,0,4,26,0,7]};
| [reply] [d/l] [select] |
Re: hash of arrays or separate hashes?
by TomDLux (Vicar) on Mar 04, 2004 at 16:33 UTC
|
Naming Fields Correctly
The first value is sum or total, not count. Alpha, Beta, and Gamma all have a count of six, if you mean the number of elements. You could also call that the size. You could also say the count of all three is 3, as that is the number of non-zero elements.
The difference won't make your program crash, but using the right term leads to code that is easier to understand. As well, using the wrong term encourages using the value for what it's called, rather than what it really contains.
Dependent Data
There is always a risk in storing two different views of the same data. Having both the data values and the sum of the values requires that one be updated whenever the other changes. Is it possible to calculate the sum only when you need it, or are the uses widely separated, yet frequent? After all, a subroutine to calculate the sum would be simple and quick.
On the other hand, if you read in the values and know they cannot change, and use the sum frequently, caching the total in the hash may be perfectly appropriate.
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] |