8 posts
Posted 24 February 2012 - 05:46 PM
Hey,
i found this mod yesterday and im very impressed of it.
Now i am trying to make my own scripts.
The problem is: I want to split a string into chars.
Like: var = 13121012
To: var[1] = 1 var[2] = 3 … var[8] = 2
I am used to Java. ( i started learning lua after i found this mod )
- var.toCharArray();
Now i need the same function for lua, but i cant find it :/
Does anybody know it?
Thanks a lot, bepZ
454 posts
Location
London
Posted 24 February 2012 - 05:59 PM
I'm assuming you mean this:
var = "13121012"
You can use the following to obtain characters of it:
string.sub(var, 1, 1) -- Will return "1"
var:sub(2, 2) -- Will return "3"
var:sub(3, 4) -- Will return "12"
Note that in order to use var:sub, the type() of var is required to be string.
You can use string.sub(number, …), as this will internally convert the number to a string.
8 posts
Posted 24 February 2012 - 06:02 PM
Thats what i was looking for, thanks!
8 posts
Posted 24 February 2012 - 08:46 PM
I got a new problem, now it says "Infinity", maybe because my string is very long.
Here is my string: 001101100011111110111111111111111111011111111111111110011111111111111111111111111011111110001101100
I want to read one char at a time.
Is there any way to solve my problem?
454 posts
Location
London
Posted 24 February 2012 - 09:09 PM
Your 'string' is probably a number, try this:
local s = tostring(your string that is probably a number)
print(s:sub(1, 3)) -- or some other parameters to sub
Edit: Just did some testing; any numbers that are long will yield "inf" or "Infinity" when turned into a string; to solve this, make sure your variable is a string to start with, and not a number.
8 posts
Posted 24 February 2012 - 09:18 PM
I am so dumb, i made a mistake.. i forgot the "" around my number..
Shame on me!
8 posts
Posted 24 February 2012 - 09:30 PM
I dont understand why the turtle is not placing the blocks. Here the script.
buildnumber = "001101100011111110111111111111111111011111111111111110011111111111111111111111111011111110001101100"
count = 1
for x = 0, 5, 1 do
for y = 0, 10, 1 do
for z = 0, 7, 1 do
write(string.sub(buildnumber, number, number))
-- block placement
kind = string.sub(buildnumber, number, number)
if kind == 1 then
turtle.placeDown(1)
elseif kind == 2 then
turtle.placeDown(2)
elseif kind == 3 then
turtle.placeDown(3)
end
-- block placement end
turtle.forward()
number = number + 1
end
for z = 0, 7, 1 do
turtle.back()
end
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
turtle.turnLeft()
for y = 0, 10, 1 do
turtle.forward()
end
turtle.turnRight()
turtle.up()
end
for x = 0, 5, 1 do
turtle.down()
end
The turtle moves where i want it to move, but it doesnt place the block.
454 posts
Location
London
Posted 24 February 2012 - 10:01 PM
I dont understand why the turtle is not placing the blocks. Here the script.
buildnumber = "001101100011111110111111111111111111011111111111111110011111111111111111111111111011111110001101100"
count = 1
for x = 0, 5, 1 do
for y = 0, 10, 1 do
for z = 0, 7, 1 do
write(string.sub(buildnumber, number, number))
-- block placement
kind = string.sub(buildnumber, number, number)
if kind == 1 then
turtle.placeDown(1)
elseif kind == 2 then
turtle.placeDown(2)
elseif kind == 3 then
turtle.placeDown(3)
end
-- block placement end
turtle.forward()
number = number + 1
end
for z = 0, 7, 1 do
turtle.back()
end
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
turtle.turnLeft()
for y = 0, 10, 1 do
turtle.forward()
end
turtle.turnRight()
turtle.up()
end
for x = 0, 5, 1 do
turtle.down()
end
The turtle moves where i want it to move, but it doesnt place the block.
You need to put quotes around your 1, 1 ~= "1"
8 posts
Posted 24 February 2012 - 10:05 PM
oh.. im blind.
thanks.
496 posts
Location
New Zealand
Posted 24 February 2012 - 10:57 PM
This will return a table containing all the chars.
function toCharTable(string)
local string = tostring(string)
local chars = {}
for n=1,#string do
chars[n] = string:sub(n,n)
end
return chars
end
vars = toCharTable(4523789)
--Will return:
--vars[1] = 4
--vars[2] = 5
--vars[3] = 2
--And So on.