Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

User input to find a word in a dictionary
Started by hayful59 Feb 10 2019 08:16 PM

4 replies to this topic
userinput dictionary find table

Best Answer XeduR @Spyric , 11 February 2019 - 09:09 PM

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 )

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

hayful59

[GLOBAL: userInfoPane.html]
hayful59
  • Enthusiast

  • 40 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 894 posts
  • Corona SDK

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.).



[TOPIC: post.html]
#3

hayful59

[GLOBAL: userInfoPane.html]
hayful59
  • Enthusiast

  • 40 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 894 posts
  • Corona SDK

  Best Answer

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 )


[TOPIC: post.html]
#5

hayful59

[GLOBAL: userInfoPane.html]
hayful59
  • Enthusiast

  • 40 posts
  • Corona SDK

 

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




[topic_controls]
[/topic_controls]