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

Splitting Strings

Started by svdragster, 30 January 2013 - 07:04 AM
svdragster #1
Posted 30 January 2013 - 08:04 AM
Hello,

I know there are many posts about this, but I don't understand a single word, so I hope someone can explain me some stuff.

I want a program to check if in the startup file exists a line with "shell.run". (Anything can be behind it)

How is that possible?
Mads #2
Posted 30 January 2013 - 08:12 AM
You would do something like this:



local f = io.open("/startup", "r") -- open the startup file for reading
local data = f:read("*a") -- read the entire file
f:close() -- close the file

if data:find("shell.run") then -- check if "shell.run" exists in the file contents.
    print("Found a line with shell.run!")
end
svdragster #3
Posted 30 January 2013 - 08:17 AM
Thanks ~_~
Mads #4
Posted 30 January 2013 - 08:17 AM
Btw, it has nothing to do with splitting strings :)/>
svdragster #5
Posted 30 January 2013 - 08:19 AM
xD Fail, well that's the only thing I found while searching for something like this…
Kingdaro #6
Posted 30 January 2013 - 08:20 AM
IIRC, :find() uses pattern matching, so you should probably escape that dot when looking for shell.run.


if data:find("shell%.run") then
Mads #7
Posted 30 January 2013 - 08:27 AM
Would be

if data:find("shell\.run") then
Orwell #8
Posted 30 January 2013 - 08:37 AM
Would be

if data:find("shell\.run") then
No, patterns in lua are escaped with a %. It's not the same as escaping strings. It's just indicating that the character (dot) is not a magic symbol, but just a plain dot.