Jump to content




Input not worry about capitals etc.


  • You cannot reply to this topic
10 replies to this topic

#1 koslas

  • Members
  • 62 posts
  • LocationChristchurch, New Zealand

Posted 21 January 2013 - 01:06 AM

I have a quick question, How am I able to make it so if you input something, it doesn't matter about capitals and stuff
For example this requires you to add "if input == "yes" or input == "Yes" etc.
If I were to type "YEs" with this lot of code it would error

Spoiler

But I want it so I would only need to do this lot of code with 'if input == "yes" then' and it would work with any sort of capilization.

Spoiler

So I would only need to type yes/no once not over and over with all the different combinations for yes/no
Please tell me you understand me

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 January 2013 - 01:07 AM

Using lower() on the input string will turn all the characters into their lowercase equivalent...

example:
input = read()
input = input:lower()
if input == "yes" then
-- etc


#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 21 January 2013 - 01:07 AM

string.lower(read()) to make it all into lower-case


told = false
while not told do
  print("Would you like to open the door?")
  input = string.lower(read())
  if input == "yes" then
        rs.setOutput("left", true)
        sleep(3)
        rs.setOutput("left", false)
        told = true
  elseif input == "no" then
        rs.setOutput("left", false)
        told = true
  else
        print("Did not reconise")
  end
end


#4 koslas

  • Members
  • 62 posts
  • LocationChristchurch, New Zealand

Posted 21 January 2013 - 01:18 AM

Also how would I make it so if you entered a number in a small quiz like this:
Spoiler
I would have to have the two with commas, as some people use commas in big numbers (every 3rd number from the right) so it wouldn't worry if they did with just the one for example:

Spoiler
And I entered 3,000 in this second program?

#5 koslas

  • Members
  • 62 posts
  • LocationChristchurch, New Zealand

Posted 21 January 2013 - 03:33 AM

And also to see if I entered a certain phrase into something so if I needed the answer of True
It would also accept "It is true" as long as it has "true" somewhere in the string?

#6 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 21 January 2013 - 04:50 AM

print("What is 1000x3")
input = tonumber(read())

print(input == 3000 and "Correct!" or "Incorrect!") -- advanced way to print


#7 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 21 January 2013 - 04:53 AM

string.match works here:

input = read()
if input:lower():match('true') then
    -- string contains true
end


#8 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 21 January 2013 - 04:59 AM

View Postkoslas, on 21 January 2013 - 01:18 AM, said:

Also how would I make it so if you entered a number in a small quiz like this:
...
And I entered 3,000 in this second program?

View Postkoslas, on 21 January 2013 - 03:33 AM, said:

And also to see if I entered a certain phrase into something so if I needed the answer of True
It would also accept "It is true" as long as it has "true" somewhere in the string?

Both can be done using the Lua string library.

1. You could use the string method gsub. string.gsub replaces all occurrences of a string with another. In this case, you could replace all commas with nothing using the code:
input = read():lower()
input = string.gsub(input, ",", "")

-- OR using a different notation:

input = read():lower():gsub(",", "")
You could also use this to get rid of spaces and other unwanted characters.
2. You can use string.find for this one. String.find searches a string and sees whether a substring exists in it, and if it does exist, it returns its position. Example:
input = read():lower()
if string.find(input, "true") then
  -- true was found in the string!
end

-- OR using a different notation:

input = read():lower()
if input:find("true") then
  -- found!!
end


#9 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 21 January 2013 - 05:03 AM

View PostGravityScore, on 21 January 2013 - 04:59 AM, said:

1. You could use the string method gsub. string.gsub replaces all occurrences of a string with another. In this case, you could replace all commas with nothing using the code:
input = read():lower()
input = string.gsub(input, ",", " ")
You could also use this to get rid of spaces and other unwanted characters.
2. You can use string.find for this one. String.find searches a string and sees whether a substring exists in it, and if it does exist, it returns its position. Example:
input = read():lower()
if string.find(input, "true") then
  -- true was found in the string!
end

Don't forget the more idiomatic and concise notation of input:gsub(",", " ") and input:find('true')

#10 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 21 January 2013 - 05:08 AM

View PostEric, on 21 January 2013 - 05:03 AM, said:

Don't forget the more idiomatic and concise notation of input:gsub(",", " ") and input:find('true')

I always use that one myself, but I thought using the string.meh notation was clearer for this example :P

I'll edit it in...

#11 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 January 2013 - 05:40 AM

It's true that the use of the string metatable to call the methods directly on the string is perfectly acceptable and more concise, but for the purposes of Ask a Pro answers, it's usually best to use the string.func() notation primarily and add a note (if desired) that the more concise method is available, since it leaves the question-asker the option to use the potentially easier to understand less-concise option while also pointing out the more-concise (but perhaps slightly more difficult to follow) option.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users