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

Creating a Train controller system using a network

Started by Cassem, 04 June 2012 - 09:56 PM
Cassem #1
Posted 04 June 2012 - 11:56 PM
Hello all,

Im relatively new to programming with LUA and i was trying to program a network with packet switching and noticed Monochrome
( http://www.computercraft.info/forums2/index.php?/topic/527-packet-switched-networking-cover-big-distances-with-wireless/ )
had already created such a network. As far as i can tell it works extremely well, and now here is my problem. I modified his code to create a new independent network
for tracking information about my railway system and routing railcarts to destinations that i input.

to accomplish this i added a new table that stores a string in the form of <label> ">" <computer ID>
i can get the string to be added to the table with this code
--puts the label and id into one string for use with the processing function **CASSEM ADDED**
local function merge_LabelandID(label, id)
str = label .. ">" .. id .. " "
print("Associated label " .. label .. " with " .. id)
Label[id] = str
end

and i can get the string to be added to a packet to be sent to other gateways with this code,

--Adds the label and associated ID to the gwlist
for id3, labels in pairs(Label) do
   ret = ret .. labels .. " "
end

but when i try to process the packet on a gateway it tells me this chunk of code

--Process label list **CASSEM ADDED**
  pos = 0
for st,sp in function() return string.find(labellist," ",pos,true) end do
  entry = string.sub(labellist,pos,st-1) .. ""

  -- labels is the label, id is the id with which it's associated
  labels = string.sub(entry, 0, (string.find(entry, ">") - 1)).. ""
  id = tonumber(string.sub(entry, string.find(entry, ">") + 1))

  if (Label[id] == nil)
  then
   -- Update Label list. We are now dirty
   Label[id] = merge_LabelandID(labels, id)
   dirty = true
  end

  pos = sp + 1 -- Jump past current divider
end

more specifically

labels = string.sub(entry, 0, (string.find(entry, ">") - 1)).. ""

is producing this error
"attempt to perform arithmetic __ sub on nil and number"

and needless to say im at a loss if you feel you need more code to look at to help identify the problem, I'm more than willing to provide it
Xfel #2
Posted 05 June 2012 - 07:45 AM
string.find returns nil if it doesn't find the searched string. try printing out each entry and then you should be able to find the error. (or paste the output here and someone will tell you)
Cassem #3
Posted 06 June 2012 - 09:44 AM
Ok so ive found out a bit more information about what is going wrong in my program.

this is the updated code for the part that processes the label list
--Process label list **CASSEM ADDED**
  pos = 0
for st,sp in function() return string.find(labellist," ",pos,true) end do
  entry = string.sub(labellist,pos,st-1) .. ""
  print(entry)

  -- labels is the label, id is the id with which it's associated
  if ((string.find(entry, ">") -1) == nil)
  then
   print(" string.find is equal to nil")
  else
   print(" This is the position of the last letter of the label " .. string.find(entry, ">") - 1)
   labels = string.sub(entry, 0, (string.find(entry, ">") - 1)) .. ""
  end
  print("this is after labels is assigned in the processing function")
  id = tonumber(string.sub(entry, string.find(entry, ">") + 1))
  print(Label[id])

  if (Label[id] == nil)
  then
   -- Update Label list. We are now dirty
   merge_LabelandID(labels, id)
   print(Label[id])
   dirty = true
  end

  pos = sp + 1 -- Jump past current divider
end

The loop finishes once without a hitch the last print(Label[id}) before the dirty = true bit prints the array and it shows that there is one string inside the array
which is to be excpected. once the loop starts again it gets to this part of the code


  if ((string.find(entry, ">") -1) == nil)
  then
   print(" string.find is equal to nil")
  else
   print(" This is the position of the last letter of the label " .. string.find(entry, ">") - 1)
   labels = string.sub(entry, 0, (string.find(entry, ">") - 1)) .. ""

and never does the first part of the If, it always goes to the else statement and generates the same error
"Attempt to perform arithmetic __ sub on nil and number." this has me wondering if the increments my parser is producing are off by a few places.

this is the code that seperates the initial packet into managable chunks that mean something
the packet is in the form of <gateway_list>:<host_list>/<label_list>|<master_computer_id>

-- Separate in two parts; gwlist and hostlist
gwlist = string.sub(str, 0, string.find(str, ":") - 1)
hostlist = string.sub(str, string.find(str, ":") + 1, string.find(str, "/")-1)
hostlist = hostlist .. ""

--Associating labels with ID's **CASSEM ADDED**
labellist =string.sub(str, string.find(str,"/")+1, string.find(str, "|") -1)
labellist = labellist .. ""

--part for passing Master ID amoung gateways ** CASSEM ADDED**
masterid = string.sub(str, string.find(str, "|") + 1)