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

Limiting amount of characters to type?

Started by brett122798, 29 October 2012 - 03:15 AM
brett122798 #1
Posted 29 October 2012 - 04:15 AM
Hi, I want to know how I could make an input limited on characters.



Side Question: How can I measure the amount of characters in a variable?
ChunLing #2
Posted 29 October 2012 - 05:29 AM
#varname returns the number of characters in a string or elements in the top layer of a table.

So, you can do a couple of things. You can have a repeat varname = read() until #varname < limit, you can just get the input and then if #varname < limit then varname = varname:sub(1,limit) end, or a number of other things. The loop is good if you want to make sure that the user inputs the shorter string, the other okay if you just want the string shortened for some reason.
remiX #3
Posted 29 October 2012 - 06:33 PM
Yeah so something like this

term.clear() term.setCursorPos(1,1)

repeat
    var = read()
until #var < 11

print(var)
ChunLing #4
Posted 29 October 2012 - 08:33 PM
Well…you do want to have a prompt that says something about the 10 character limit. I don't know if you would want to put that in the loop or before it, just as long as it's somewhere so the user knows what they're supposed to do.