This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
subzero22's profile picture

make read() all lower case?

Started by subzero22, 06 January 2014 - 08:15 PM
subzero22 #1
Posted 06 January 2014 - 09:15 PM
Is there a way to make it so when someone has to type something in it makes it all lower case in case they put some caps in there?

here's a snip of the code I'm using.


print("Build left or right?")
write("> ")
left = read()
print("")

print("would you like the turlte to dig the floor also? yes/no")
write("> ")
dig = read()
print("")

full program incase you want to see it http://pastebin.com/Y9ZwQkgq
Lyqyd #2
Posted 06 January 2014 - 10:18 PM
string.lower takes a string and returns an all-lower-case version of that string.
oeed #3
Posted 06 January 2014 - 10:39 PM
Going on from what Lyqyd said,

You can use string.lower, so for example,


print("Build left or right?")
write("> ")
left = string.lower(read())
print("")

If you want it to be lowercase as you type (for example, if you typed 'Apple' it would display 'apple') you'd need to modify the read function and use string.lower on the character input.
subzero22 #4
Posted 07 January 2014 - 02:42 AM
ok thanks for the help :)/>
subzero22 #5
Posted 07 January 2014 - 05:09 AM
sadly I cant test right now but would I also need to change the if statements or would they stay the same?

I also have another question but kinda off topic. For turtle event types for like a refuel script could I do something like this?



if turtle.getFuelLevel() <= fuel then
print("Turtle has "..flvl.." fuel and needs "..fuel.."fuel to strip this much.")
print("Please put fuel in slot 1.")

-- would I use this like in the wiki?

os.pullEvent("turtle_inventory")

-- or would I have to add something else?

turtle.refuel(64)
else
print("Stripmine Starting")
sleep(1)
end
Bomb Bloke #6
Posted 07 January 2014 - 05:36 AM
You might want to throw in a turtle.select() to make sure slot 1 is selected (for eg, remember that restarting the script won't reset the current slot), and maybe a while loop to ensure it actually gets refueled, but yeah, the line works more or less as you seem to think it does.

turtle.select(1)
if turtle.getFuelLevel() < fuel then
  print("Turtle has "..turtle.getFuelLevel().."/"..fuel.." required fuel.")
  turtle.refuel()

  while turtle.getFuelLevel() < fuel then
    -- After the last attempt to refuel, we still don't have enough.
    print("Please put more fuel in slot 1 (needs "..(fuel-turtle.getFuelLevel()).." more).")
    os.pullEvent("turtle_inventory")
    turtle.refuel()
  end
end

print("Have "..turtle.getFuelLevel().."/"..fuel.." fuel, stripmine starting...")

Regarding the "if" statements, since you can be sure the "left" variable holds a lower-case string, there's no need to pull anything fancy there. This is fine for eg:

left = string.lower(read())
if left == "left" then
.
.
.
subzero22 #7
Posted 07 January 2014 - 06:28 PM
ok thanks