Posted 21 July 2012 - 03:00 PM
Hi, I want to make some kind of server program, which receives commands over rednet and executes them. I wrote a function which seperates the command and its parameters. It looks like this:
This function should first write down the position of each space in the table spaces. Then it should write the command and each parameter in a field of cmd and return cmd. This function returned weird results so I added two print() commands to the function, it looks like this:
Now it should every time it goes trough the loop print the message it receives when the function is called and the position of the space it found. But when I send the message "login testpass", it prints out 10 as the space position. The message doesn't change, so it seems like string.find() receives correct parameters but returns a wrong position.
I copied the function into a new file and changed really nothing, but when I used the copied function it worked right. In this test the program red the command from the terminal, normally it receives it per rednet.
I don't understand why the function acts like this and hope you can help me.
Palaton
function getCmd(message)
cmd = {}
spaces = {}
pos = 0
j = 0
while pos ~= nil do
pos = string.find(message, " ", pos + 1, true)
if pos ~= nil then
spaces[j] = pos
j = j + 1
end
end
length = string.len(message)
spacesX = {0}
for i = 1, #spaces do
spacesX[i + 1] = spaces[i]
end
spacesX[#spaces + 2] = length + 1
for i = 1, #spaces + 1 do
cmd[i] = string.sub(message, spacesX[i] + 1, spacesX[i + 1] - 1)
end
return cmd
end
This function should first write down the position of each space in the table spaces. Then it should write the command and each parameter in a field of cmd and return cmd. This function returned weird results so I added two print() commands to the function, it looks like this:
function getCmd(message)
cmd = {}
spaces = {}
pos = 0
j = 0
while pos ~= nil do
print(message)
pos = string.find(message, " ", pos + 1, true)
if pos ~= nil then
print(pos)
spaces[j] = pos
j = j + 1
end
end
length = string.len(message)
spacesX = {0}
for i = 1, #spaces do
spacesX[i + 1] = spaces[i]
end
spacesX[#spaces + 2] = length + 1
for i = 1, #spaces + 1 do
cmd[i] = string.sub(message, spacesX[i] + 1, spacesX[i + 1] - 1)
end
return cmd
end
Now it should every time it goes trough the loop print the message it receives when the function is called and the position of the space it found. But when I send the message "login testpass", it prints out 10 as the space position. The message doesn't change, so it seems like string.find() receives correct parameters but returns a wrong position.
I copied the function into a new file and changed really nothing, but when I used the copied function it worked right. In this test the program red the command from the terminal, normally it receives it per rednet.
I don't understand why the function acts like this and hope you can help me.
Palaton