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

Cutting strings?

Started by Apfeldstrudel, 27 June 2013 - 09:03 AM
Apfeldstrudel #1
Posted 27 June 2013 - 11:03 AM
Hello, i have a question- what is the best way to "cut" strings? For loop with set/get char? String.separate?(i will store variables in a file with the syntax "something:value")

Is there an easier way to achieve this?
Apfeldstrudel #2
Posted 27 June 2013 - 04:43 PM
Anyone?
GopherAtl #3
Posted 27 June 2013 - 05:27 PM
there is no string.separate in lua, nor is there anything to set individual characters in strings. Lua's string library is pretty basic, really, with the exception of the lua pattern functions. Patterns could let you do this, but in many cases, it's easier to just use textutils.serialize(), which will convert a whole table into a string which can be written to a file, then load it back and pass it to textutils.unserialize(), which will give the table back. If you've got a lot of variables to save, you can just make a table to shove them all into first.

example:


--some state variables. Initialize to the values you'd use the first time it's run on a computer
local curIndex=4
local miscInfo={}
local username="bob"

local function saveState()
  local state={}
  state.curIndex=curIndex
  state.miscInfo=miscInfo
  state.username=username
  local str=textutils.serialize(state)
  local file=fs.open("mystate","w")
  file.write(str)
  file.close()
end

local function loadState()
  local file=fs.open("mystate","r")
  --//first run, there won't be a file, so check first.
  if file then
	local str=file.readAll()
	local state=textutils.unserialize(str)
	curIndex=state.curIndex
	miscInfo=state.miscInfo
	username=state.username
  end
end
Apfeldstrudel #4
Posted 28 June 2013 - 04:52 AM
I think that serialize was confusing- so i read up on patterns


r=Fs.open("filename","r")
Age=tonumber(String.find(r.readLine(1),"%d"))
Would this work?(for ints)
GopherAtl #5
Posted 28 June 2013 - 12:15 PM
you think patterns are less confusing than textutils.serialize/unserialize? O_o


Your pattern there, "%d", would pull only a single digit, 0-9, from the number. If the line were "123", it would give you only 1.
You probably want "%d+", which will match one or more digits.

Also, you use string.find, which does not return the thing it found, but the index it found it /at/. You'll want string.match instead.

Also also, you could've, in far less time than it took to get a response here, gone to a cc computer or your cc emulator of choice, run lua, and typed those two lines of code and just seen what happened!

And lastly, you're still typing "String" instead of "string." Lua is case-sensitive. string has the string functions. String is nil.

Protip on programming in general: there is no "blow up the world" instruction. You do not need a high confidence your program will work before you take the risk of running it; just run it, and see what happens, then try to fix it until it doesn't need fixing anymore. The earlier and more frequently you do this, the less painful debugging will be. If you're not sure how to use some language feature, try it in the lua interpreter before putting it in your program. Fiddle. Experiment. You will never learn if you're afraid to run the code and get error messages.
Apfeldstrudel #6
Posted 29 June 2013 - 05:27 AM
I know, but i dont have access to a computer until next week

Its String in java(and im typing it on my iPhone)

Im learning lua and i think patterns are more useful to learn
0099 #7
Posted 30 June 2013 - 03:31 PM

file = fs.open("file","r") -- your file
t = {} -- table to store values
while true do
  s = file.readLine()
  if s == nil then break end
  _,_,key,value = string.find("(%w+):(/>%w+)")
  t[key] = value
end

Here () in pattern are capture characters, all in () will be captured and returned by string.find() after result coordinates ( _ is a "garbage variable", i don't need to save result coordinates )

More about captures: http://www.lua.org/pil/20.3.html

P.S. I have a small library (written by me) to access ini-like files ("key = value" format): http://pastebin.com/ii2xHtjE