User input to find a word in a dictionary

Im fairly new to programming and was wondering if anyone could point me in the right direction?

Im trying to take the users input and use that to find that word in the dictionary e.g If I type in “jeff” itll look in the dictionary and tell me “12” because        cluthaTable[“jeff”] = “12”;

cluthaTable = {}; cluthaTable["jeff"] = "12"; local defaultField local function textListener( event ) if ( event.phase == "ended" or event.phase == "submitted" ) then -- Output resulting text from "defaultField" --print( event.target.text ) -- Saves the input as a var for looking up name local test = event.target.text print(test) end end -- Create text field defaultField = native.newTextField( 150, 150, 180, 30 ) defaultField:addEventListener( "userInput", textListener )

Any and all help is much appreciated, Thank you in advance.

It really depends on whether you are planning on creating a dictionary by yourself, like you have in your sample code, or if you are planning on downloading some text file with tens of thousands or hundred(s) (of) thousand(s) (of) words.

The simplest approach, if you are planning on creating a dictionary by yourself, could be something like this:
 

local t = { hello = 5, my = 3, fellow = 10, gentlemen = 20, } local word = "gentlemen" print(t[word])

t is a table containing words as value pair entries. In lua, you can access tables’ contents using two methods, either, for example:
t.gentlemen, or
t[“gentlemen”]

Both methods access the same entry, gentlemen, and retrieve the entry’s associated value, so the print outputs “20”. So, using this idea, you could, for instance, take a string of text created via text field and then check if an entry like that exists in the table.

Do note, however, that this would be far from creating an actual dictionary, which would require a different approach (most likely points based on characters instead of words, etc.).

Im making my own dictionary with around 30-40 entries in it, Im planning on have a text box where you type “jeff” then it looks in the dictionary and tells me “12”, Do you know of any links or sample code that I can look at? thanks

Well, I already gave you the sample code, all you needed to do was merge it with yours, i.e.

 

local t = { hello = 5, my = 3, fellow = 10, gentlemen = 20, jeff = 12, } local defaultField local function textListener( event ) if ( event.phase == "submitted" ) then if t[event.target.text] then print( event.target.text.. " exists in the dictionary and is worth "..t[event.target.text].." points." ) else print( event.target.text.." doesn't exist in the dictionary." ) end end end defaultField = native.newTextField( 150, 150, 180, 30 ) defaultField:addEventListener( "userInput", textListener )

thank you for the help, it works a treat, I appreciate it

It really depends on whether you are planning on creating a dictionary by yourself, like you have in your sample code, or if you are planning on downloading some text file with tens of thousands or hundred(s) (of) thousand(s) (of) words.

The simplest approach, if you are planning on creating a dictionary by yourself, could be something like this:
 

local t = { hello = 5, my = 3, fellow = 10, gentlemen = 20, } local word = "gentlemen" print(t[word])

t is a table containing words as value pair entries. In lua, you can access tables’ contents using two methods, either, for example:
t.gentlemen, or
t[“gentlemen”]

Both methods access the same entry, gentlemen, and retrieve the entry’s associated value, so the print outputs “20”. So, using this idea, you could, for instance, take a string of text created via text field and then check if an entry like that exists in the table.

Do note, however, that this would be far from creating an actual dictionary, which would require a different approach (most likely points based on characters instead of words, etc.).

Im making my own dictionary with around 30-40 entries in it, Im planning on have a text box where you type “jeff” then it looks in the dictionary and tells me “12”, Do you know of any links or sample code that I can look at? thanks

Well, I already gave you the sample code, all you needed to do was merge it with yours, i.e.

 

local t = { hello = 5, my = 3, fellow = 10, gentlemen = 20, jeff = 12, } local defaultField local function textListener( event ) if ( event.phase == "submitted" ) then if t[event.target.text] then print( event.target.text.. " exists in the dictionary and is worth "..t[event.target.text].." points." ) else print( event.target.text.." doesn't exist in the dictionary." ) end end end defaultField = native.newTextField( 150, 150, 180, 30 ) defaultField:addEventListener( "userInput", textListener )

thank you for the help, it works a treat, I appreciate it