Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

confused with hash maps

Status
Not open for further replies.

stort

Programmer
Feb 17, 2006
10
GB
hi i've got a problem with hash maps

currently i've got a hash map holding words as the keys and count (the number of times the word occurs) as the value.

i would also like to add another column called weights so it would look like

word#1 count#1 weight#1
word#2 count#2 weight#2
word#3 count#3 weight#3
word#4 count#4 weight#4
etc etc etc

i've heard of a couple of ways to do this:
i could either have a single hash map holding words (key) and a 2 element array (value), where the array holds 2 integers, count and weight.

or have two hash maps
one holding words (key) and count (value) and the other holding words (key) and weight (value). and combine them when they are called.

if there is any other simpler way of doing this i would love to know it. if not i am totally stuck as to how to implement either of the above ideas. so a few pointers would be great

finally count needs to be multipled by weight to give a total

cheers
 
In my opinion, unless you need to do advanced sorting later, it's probably just as easy using a typical hash using delimiters.

$hash{'word'} = "20:15lbs";
 
a hash of hashes:

Code:
my %words = (
   word1 => {word => 'word1', count => 'count1', weight => 'weight1'},
   word2 => {word => 'word2', count => 'count2', weight => 'weight2'},
   word3 => {word => 'word3', count => 'count3', weight => 'weight3'},
   word4 => {word => 'word4', count => 'count4', weight => 'weight4'},
);

or a hash of arrays:

Code:
my %words = (
   word1 => ['count1','weight1'],
   word2 => ['count2','weight2'],
   word3 => ['count3','weight3'],
   word4 => ['count4','weight4'],
);

or an array of arrays:

Code:
my @words = (
   ['word1','count1','weight1'],
   ['word2','count2','weight2'],
   ['word3','count3','weight3'],
   ['word4','count4','weight4'],
);


iluvperls's suggestion will not work unless you use two hashes as it's not storing the count, only the weight of the word.
 
You forgot and array of hashes ;-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top