Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to only get the output of a table?
Started by hayful59 Feb 19 2019 06:30 PM

4 replies to this topic
table array output event lower

Best Answer XeduR @Spyric , 20 February 2019 - 12:01 AM

If you want to turn a string all lowercase, you'd use string.lower(s) or s:lower(). Lua is case sensitive when it comes to variable names, so what you are doing is creating two entries in your table: "Jack and Jack" and "jack and jack". If you plan on creating only one variable, you need to turn the string lowercase before you create the entry.

First, you create Jack and Jack.

testTable["Jack and Jack"] = "200"
print(testTable["Jack and Jack"])
--> output: 200 (string, not a number)

Then you create jack and jack.

testTable["jack and jack"] = "Jack and Jack = 200"
print(testTable["jack and jack"])
--> output: Jack and Jack = 200 (string)

Finally, when you print out event.target.text and the entry, you are printing out:

print( event.target.text.. " = "..testTable[event.target.text] )
-> event.target.text = "jack and jack"
-> testTable[event.target.text] = "Jack and Jack = 200"

So, your code is really outputting exactly what you wrote. Since you didn't explicitly state what your expected output is, then I'm just going to guess that your problem is the creation of two entries instead of one, as well as setting the next entry to be "Jack and Jack = 200", which then leads to odd looking final output when you include the event.target.text.. " = " bit.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

hayful59

[GLOBAL: userInfoPane.html]
hayful59
  • Enthusiast

  • 39 posts
  • Corona SDK

Hello, Im trying to convert what a user has typed to all lowercase then taking that lowercase entry, searhing it through a table, is there anyway to only get the second half of the table and not the whole thing.

This is what I have.

testTable["Jack and Jack"] = "200";

This is what Im trying to do so if you type in "jack and jack" (all lowercase) then it only returns "Jack and Jack = 200"

testTable["jack and jack"] = "Jack and Jack = 200";

it currently returns "jack and jack = Jack and Jack = 200"

print( event.target.text.. " = "..testTable[event.target.text] )
                print(testTable[event.target.text])

This is what I use to search ^

 

Any ideas thanks



[TOPIC: post.html]
#2

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Corona Geek

  • 1,130 posts
  • Corona SDK

What does this 

print(testTable[event.target.text])

return?

 

It should return what you are looking for.



[TOPIC: post.html]
#3

hayful59

[GLOBAL: userInfoPane.html]
hayful59
  • Enthusiast

  • 39 posts
  • Corona SDK

What does this 

print(testTable[event.target.text])

return?

 

It should return what you are looking for.

 

it returns "jack and jack = Jack and Jack = 200"



[TOPIC: post.html]
#4

XeduR @Spyric

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

  • 716 posts
  • Corona SDK

  Best Answer

If you want to turn a string all lowercase, you'd use string.lower(s) or s:lower(). Lua is case sensitive when it comes to variable names, so what you are doing is creating two entries in your table: "Jack and Jack" and "jack and jack". If you plan on creating only one variable, you need to turn the string lowercase before you create the entry.

First, you create Jack and Jack.

testTable["Jack and Jack"] = "200"
print(testTable["Jack and Jack"])
--> output: 200 (string, not a number)

Then you create jack and jack.

testTable["jack and jack"] = "Jack and Jack = 200"
print(testTable["jack and jack"])
--> output: Jack and Jack = 200 (string)

Finally, when you print out event.target.text and the entry, you are printing out:

print( event.target.text.. " = "..testTable[event.target.text] )
-> event.target.text = "jack and jack"
-> testTable[event.target.text] = "Jack and Jack = 200"

So, your code is really outputting exactly what you wrote. Since you didn't explicitly state what your expected output is, then I'm just going to guess that your problem is the creation of two entries instead of one, as well as setting the next entry to be "Jack and Jack = 200", which then leads to odd looking final output when you include the event.target.text.. " = " bit.


  • hayful59 likes this

[TOPIC: post.html]
#5

hayful59

[GLOBAL: userInfoPane.html]
hayful59
  • Enthusiast

  • 39 posts
  • Corona SDK

If you want to turn a string all lowercase, you'd use string.lower(s) or s:lower(). Lua is case sensitive when it comes to variable names, so what you are doing is creating two entries in your table: "Jack and Jack" and "jack and jack". If you plan on creating only one variable, you need to turn the string lowercase before you create the entry.

First, you create Jack and Jack.

testTable["Jack and Jack"] = "200"
print(testTable["Jack and Jack"])
--> output: 200 (string, not a number)

Then you create jack and jack.

testTable["jack and jack"] = "Jack and Jack = 200"
print(testTable["jack and jack"])
--> output: Jack and Jack = 200 (string)

Finally, when you print out event.target.text and the entry, you are printing out:

print( event.target.text.. " = "..testTable[event.target.text] )
-> event.target.text = "jack and jack"
-> testTable[event.target.text] = "Jack and Jack = 200"

So, your code is really outputting exactly what you wrote. Since you didn't explicitly state what your expected output is, then I'm just going to guess that your problem is the creation of two entries instead of one, as well as setting the next entry to be "Jack and Jack = 200", which then leads to odd looking final output when you include the event.target.text.. " = " bit.

You were right, thanks for the help, very clear to understand :P




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: table, array, output, event, lower