Thanks in Advance
- Adam
function maxread(len)
print "please enter Text"
mytext = read()
if #mytext > len then
while #mytext > len do
print ("maximum number of chars is " ..len)
mytext = read()
end
else
return mytext
end
end
test = maxread(5)
print(test)
That wouldn't quite work. you would need a loop in there to force it to read again when its not equal. however, from a usability point of view it would be better to just stop them from entering more text, over expecting them to count out how many they have used.very simple workaround.
for a complex function you woul have to look at os.pullEventfunction maxread(len) print "Bitte text eingeben" mytext = read() if #mytext > len then print ("maximum number of chars is " ..len) maxread(len) end end maxread(5)
That wouldn't quite work. you would need a loop in there to force it to read again when its not equal. however, from a usability point of view it would be better to just stop them from entering more text, over expecting them to count out how many they have used.very simple workaround.
for a complex function you woul have to look at os.pullEventfunction maxread(len) print "Bitte text eingeben" mytext = read() if #mytext > len then print ("maximum number of chars is " ..len) maxread(len) end end maxread(5)
function limitRead( nLength, cReplaceChar )
term.setCursorBlink( true )
nLength = nLength or -1 -- -1 is unlimited
sReturnString = ""
xPos, yPos = term.getCursorPos()
while true do
event, char = os.pullEvent()
if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check
if event == "char" then sReturnString = sReturnString .. char
elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
elseif event == "key" and char == 14 then -- Backspace
term.setCursorPos( xPos, yPos )
term.write( string.rep( " ", string.len( sReturnString ) ) )
sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
term.setCursorPos( xPos, yPos )
if not cReplaceChar then term.write( sReturnString )
else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
end
term.setCursorPos( xPos, yPos )
term.write( string.rep( " ", string.len( sReturnString ) ) )
term.setCursorPos( xPos, yPos )
if not cReplaceChar then term.write( sReturnString )
else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
end
end
Calling the function instead of using a loop causes problems. you can fill the program stack and get a 'java vm IndexOutOfBoundsException 255'. you should never use recursion unless it is 100% required, here it is not. also, you may call the function, but you never actually return the value it reads, which is another issue, its a fairly useless read function if it cannot return what it has read.Why i need a loop when the function calls itself when the if statement is true?
for the usability you are absolutely right. just wanted to show a simple option and point into a direction for more complex way.
function limitRead(nLimit, replaceChar)
term.setCursorBlink(true)
local cX, cY = term.getCursorPos()
local rString = ""
if replaceChar == "" then replaceChar = nil end
repeat
local event, p1 = os.pullEvent()
if event == "char" then
-- Character event
if #rString + 1 <= nLimit then
rString = rString .. p1
write(replaceChar or p1)
end
elseif event == "key" and p1 == keys.backspace and #rString >= 1 then
-- Backspace
rString = string.sub(rString, 1, #rString-1)
xPos, yPos = term.getCursorPos()
term.setCursorPos(xPos-1, yPos)
write(" ")
term.setCursorPos(xPos-1, yPos)
end
until event == "key" and p1 == keys.enter
term.setCursorBlink(false)
print() -- Skip to the next line after clicking enter.
return rString
end
-- And you call it like this
input = limitRead(10)
-- If you want to replace it with a char, like read("*") then
password = limitRead(10, "*")
Exactly, when you have a condition for it to finish (and that condition has it run in less calls than the stack) then its fine. Hell try to iterate a file system or solve Towers of Hanoi without using recursion! Sometimes it is just needed!You can also use a recursion when the depth of the recursion is naturally limited. The problem is when it is entirely possible (or inevitable) for the recursion to continue until the stack overflows, as is the case where a recursion is used in place of a loop.