How do i add a string index with a variable to a table?
3 replies, posted
I'm trying to make a script that records words used in chat, and how many times they are used.
For testing, i did:
[code]
Words = {"lol"}
WordCount = {["lol"] = 1}
[/code]
This worked, when i used the printing command, the most used word would be lol, used 1 times. As long as i said lol, the count would increase.
However, if i used a word i hadn't defined in the count list, it wouldn't work. How would i add a table entry with a string index (if that's what it's called) that has a value?
[code]if not table.HasValue(Words, wordvar) then
table.insert(Words , wordvar)
table.insert(WordCount , {wordvar , 1})
end[/code]
I would think..
[lua]Words[ name ] = value;[/lua]
You don't need separate tables.
Ok, thanks.... and i did realize i didn't need two tables...
[editline]10:34PM[/editline]
[QUOTE=|FlapJack|;17067453][code]if not table.HasValue(Words, wordvar) then
table.insert(Words , wordvar)
table.insert(WordCount , {wordvar , 1})
end[/code]
I would think..[/QUOTE]
All that did was add a sub table, not a value
[editline]10:38PM[/editline]
Well, I'm sure there is a better way to do it, but for now i just did:
[code]
table.Merge( WordCount, { [word] = 1} )
[/code]
Sorry, you need to Log In to post a reply to this thread.