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

need help w/ string API?

Started by digitalEntity, 05 July 2014 - 03:16 PM
digitalEntity #1
Posted 05 July 2014 - 05:16 PM
So, obviously I need help with the String API lol.

The 2 main things I'm hoping to find are a way to measure the length of a string, and a way for a computer to determine what character is in a given location within the string.

My objective is to take a large number, led by the arbitrary number 1, and create a while loop that breaks off the last 2 digits of the number, turns each pair into alpha-numerics (they were originally put into the number as alpha-numerics, using the Key api) until all that remains is said arbitrary number 1, and the original input has been strung together. But I need the computer to know the length of the number, and I need a way to determine the value of the last 2 digits lol.

The number is given to a function, via arguments, and then pulled into the function using "input = {…}"

and then clearly the while loop would begin as "while input ~= 1 do"

… After that point I'm simply missing the two string functions I need, and a little bit of creative testing lol

Any help would be appreciated.

Sincerely,
digitalEntity
Lyqyd #2
Posted 05 July 2014 - 05:31 PM
You should check out the string manipulation section of the Lua reference manual, specifically string.sub. If it doesn't mention it there, you can also get the length of a string named str with #str.
digitalEntity #3
Posted 07 July 2014 - 04:58 PM
Well, first off, thanks for the reply.

Second, I can understand how the #string stuff works, and in the time between reading your reply and writing this, I've already figured out (my opinion of) the best way to integrate it into my code. However… the vocabulary/jargon used in the reference manual is like Greek to me. could you explain how the string.sub function works, please?

I don't mean to be a pain, and I really do appreciate the help you and the other members are providing.
KingofGamesYami #4
Posted 07 July 2014 - 05:59 PM
syntax: string.sub( str, nStart, nEnd )
returns: a string from nStart to nEnd
example:

local s = string.sub( "hello", 1, 1 )
--#s now = "h"
local s = string.sub( "hello", 1, 2 )
--#s now = "he"
local s = string.sub( "hello", 5, 5 )
--#s now = "o"
Alternatively, you can use this syntax: str:sub( nStart, nEnd ), where str is a variable string. example:

local str = "hello"
local s = str:sub( 1, 1 )
--#s now = "h"

For practice & debugging, print the results

Edit: fixed formatting
Edited on 07 July 2014 - 04:01 PM
digitalEntity #5
Posted 07 July 2014 - 06:07 PM
Well then… Interesting and easy to understand… if only the people making the Lua guide did it this way lol.

Thank you for the help guys. With a little bit of planning, I can probably work this into my programming.