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!

setting variable values to zero

Status
Not open for further replies.

stort

Programmer
Feb 17, 2006
10
GB
i currently have a hash map that counts the number of word occurances (over multiple files) but cumulatively. so i need a way of resetting the value to zero

this line increments the counter when the word is found:
$keywords{$word}++ if (exists $keywords{$word});

i have tried:
$keywords{$word}=0;
but this did not work, can anybody tell me the code to affectively do what i am trying to do?
 
it should work, there must be another problem why the hash key is not getting reset to zero.
 
hmmm just wandered over this post and read it and I might be wrong but.

If you set $keywords{$word}=0; it will never excist and will never increase cause $keywords{$words} = not true

Code:
if (!$keywords{$word}){$keywords{$word} = 1};
else { $keywords{$words}++};

with the above you can set $keywords{$words}=0;

InDenial

 
that's not correct InDenial but your thinking isn't too far off. The exists() function checks for the existence of the key, not it's return value:

Code:
my %hash =(
   foo => 0,
   bar => 1
);
	
print "Foo exists\n" if (exists($hash{'foo'}));
print "Bar exists\n" if (exists($hash{'bar'}));
print "Baz exists\n" if (exists($hash{'baz'}));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top