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

String Limit

Started by koslas, 20 August 2014 - 12:57 PM
koslas #1
Posted 20 August 2014 - 02:57 PM
I've got a program, which you can register for, and I want a limit of 15 to be in the username, but I'm not sure how to make a limit of the number or characters in a string, I've tried searching, but haven't found anything
Here is my register function


function register()
	    nSelect = 1
	    clear()
	    border()
	    term.setCursorPos(2,18)
	    print("Version: "..version)
	    term.setCursorPos(1,3)
	    cPrint("Please Register")
	    cPrint("")
	    cWrite("Username: ")
	    print("")
	    cWrite("Password: ")
	    print("")
	    cWrite("Confirm Password: ")
	    term.setCursorPos(26,5)
	    user = read()
	    term.setCursorPos(26,6)
	    pass = read("*")
	    term.setCursorPos(30,7)
	    confirm = read("*")
	    cPrint("")
--	  Make Account
	    if fs.exists("/.slots/users/"..user.."/password") then
			    cPrint("Account already created")
			    sleep(2)
	    else
			    if pass == confirm then
					    fs.makeDir(".slots/users/"..user)
					    makeAccount = fs.open("/.slots/users/"..user.."/password", "w")
					    makeAccount.write(pass)
					    makeAccount.close()
					    cPrint("Account created")
					    freeSpins = 5
					    sleep(2)
					    spinLoop()
			    else
					    cPrint("Passwords do not match")
					    sleep(2)
			    end
	    end
end
theoriginalbit #2
Posted 20 August 2014 - 03:18 PM
Have a read of my post here.
koslas #3
Posted 20 August 2014 - 03:22 PM
I'm looking at your custom read function program, could you quickly explain how to use it? As it looks a bit too confusing for me, just scimming the code. And reading the description, as there is no explaination for the second arguement.
Edited on 20 August 2014 - 01:24 PM
theoriginalbit #4
Posted 20 August 2014 - 03:41 PM
The second argument, just like the ComputerCraft read function, is designed for a history playback. Basically you know how in CraftOS shell you can press up and it will show you your last run lines, that is how they are provided, through the history argument.
So a basic usage example where the history is able to be seen is the following

local history = {}
while true do
  local input = read(nil, history)
  table.insert(history, input)
end
however for this particular case you don't need to use history, so providing a nil value there will have it ignored. so reading with your requirements would be read(nil, nil, 15) to get a 15 character username using my read function.
koslas #5
Posted 20 August 2014 - 03:44 PM
Okay, that seems easy, and if I wanted a limit on a password I would do read("*", nil, 15) just as an example for a 15 character max password? Also would adding this to my code, make me need to change all my read functions that I currently use to read(nil, nil, nil) or, read("*", nil, nil)?
Edited on 20 August 2014 - 01:47 PM
theoriginalbit #6
Posted 20 August 2014 - 05:02 PM
that is precisely how you'd do a password input.

no you'd not need to change all your read functions, if you omit arguments it assumes nil, hence the fact that you don't have to supply the 4th argument in that call either. The only reason we need to specify the nil arguments is when we're wanting to supply a specific argument, such as the read limit; I deliberately designed this function override to work with ComputerCraft native read calls :)/>