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

splitting a string and store the parts in different variables.

Started by mlewelt, 08 July 2013 - 11:48 AM
mlewelt #1
Posted 08 July 2013 - 01:48 PM
Hello Again ;)/>
So i am currently working on a program that uses rednet messages as a major part.
I want to send multiple variables at one time with a message, so joining mutlitple variables in one string, sending it and then splitting it up again was my way to go.
I know how this works and it does work, but i cant figure out how to store the splitted parts again in different variables.
example:
i want to split a string

local string = "variable1 variable2"
so the string.gmatch was the way to go:

for i in string.gmatch(string, "%S+") do
 -- what to do now to store variable1 and variable2 in DIFFERENT variables again.?
end
any help is appreciated. Thanks for your answers ;)/>
Engineer #2
Posted 08 July 2013 - 01:59 PM
Most likely you are going to use a table:


local splittedStrings = {}
for s in stringName:gmatch("[%S]+") do
   table.insert( splittedStrings, s )
end

Its that easy! :)/>
btw, read up on tables if you dont know what they are
mlewelt #3
Posted 08 July 2013 - 03:02 PM
worked. thank you a lot! ;)/>
mlewelt #4
Posted 10 July 2013 - 02:16 PM
hello guys once again. xD
I just dont get why this isnt working:
I have a tabel a. in it there are stored 5 values, each of it is a string.
Then i have a second tabel, it is filled with placeholders.
I want to use the first value from tabel a as a key for tabel b.

tabelA = {"train1","U1","etc..."}
tabelB = {
			   ["train1"] = {"linie","ziel","etc..."},
			   ["trainETC..."] = {"linie","ziel","etc..."}
			   }
Now what i want is to fill "linie" from train1 of tabel B with "U1" from tabel A:

tabelB[tabelA[1]][1] = tabelA[2]
This is not working. i get an error message: attempt to index ? (nil value), so he cant find something. However i cant figure out why he cant find something.
Any help would be awesome ;)/>
Lyqyd #5
Posted 10 July 2013 - 02:18 PM
Please post the whole code and the whole error message.
mlewelt #6
Posted 10 July 2013 - 02:33 PM

local trainsBuffer = { }
trainInfo = nil
trains = {
	    ["train1"] = {"linie","ziel","station","direction"},
        ["train2"] = {"linie","ziel","station","direction"},
        ["train3"] = {"linie","ziel","station","direction"},
        ["train4"] = {"linie","ziel","station","direction"}
        }
function interpret()
 trains[trainsBuffer[1]][1] = trainsBuffer[2]
 trains[trainsBuffer[1]][2] = trainsBuffer[3]
 trains[trainsBuffer[1]][3] = trainsBuffer[4]
 trains[trainsBuffer[1]][4] = trainsBuffer[5]
end
function rednetGetInput()
 while true do
  senderId, trainInfo, distance = rednet.receive()
  print(trainInfo) print("<-- traininfo")
  for s in trainInfo:gmatch("[%S]+") do
    table.insert(trainsBuffer , s )
    print("s ist: "..s)
  end
  interpret()
  --monitor()
 end
end
rednetGetInput()
The input ( trainInfo ) is: "train1 U1 station2 backward" This input is static currently, of course it will be various later, but i am working on a big program and this is only a small chuck of the beginning. I cant post the whole program because its all a bit messy and unlogical :D/>
Engineer #7
Posted 10 July 2013 - 02:37 PM
I think this is the code you are looking for:


local tabelA = { "string"; "something"; "x" }
local tabelB = { "gnirts"; "gnihtemos"; "x" }

for k, v in pairs( tabelA ) do
  tabelB[ v ] = tabelB[ k ]
end

This will create this table:

local tableB = { 
  [1] = "gnirts"; 
  [2] = "gnihtemos"; 
  [3] = "x";
  ["string"] = "gnirts";
  ["something"] = "something";
  ["x"] = "x"
}

Edit: should have waited on the code.. ninja's.. This is an example then
mlewelt #8
Posted 10 July 2013 - 02:47 PM
im sorry but your answer really confused me right there :D/> Im not that good in programming… yet ;)/>
mlewelt #9
Posted 11 July 2013 - 02:24 PM
Hello once again.
My program is almost finished. but there is one thing i just cant figure out. i searched a long time, but all solutions i got werent the right ones.
So, here´s my problem:
I want to sort values in a table:

--this is tabelA. all the times are stored in it. the times are NOT ordered.
tabelA = {
			  ["train1"] = {"20"},
			  ["train2"] = {"60"},
			  ["train3"] = {"40"},
			  ["train4"] = {"80"}
			  }
--this is tabelB, at the end it should contain the times of all four trains. The key is for defining and the value should be the corresponding train.
tabelB = {
			 [1] = " "
			 [2] = " "
			 [3] = " "
			 [4] = " "
			 }
-- so as an example of an correctly filled tabel:
[1] = "train1"
[2] = "train3"
[3] = "train2"
[4] = "train4"
cant figure out how to sort these.
any help is appreciated ;)/>
Lyqyd #10
Posted 11 July 2013 - 02:58 PM
Use table.sort, and provide it with a function to make the desired comparison. You may find it easiest to copy the relevant data over to another table prior to performing the sort.
Lyqyd #11
Posted 11 July 2013 - 03:01 PM
Threads merged. Please stick to one topic for all questions about a specific program or piece of code.
mlewelt #12
Posted 11 July 2013 - 03:08 PM
okay. back to the prob: i kinda wanted to do something like that, but i dont know how to do it.
mlewelt #13
Posted 13 July 2013 - 06:42 AM
could somebody help me please?
albrat #14
Posted 13 July 2013 - 10:55 AM
Hello once again.
My program is almost finished. but there is one thing i just cant figure out. i searched a long time, but all solutions i got werent the right ones.
So, here´s my problem:
I want to sort values in a table:

--this is tabelA. all the times are stored in it. the times are NOT ordered.
tabelA = {
			  ["train1"] = {"20"},
			  ["train2"] = {"60"},
			  ["train3"] = {"40"},
			  ["train4"] = {"80"}
			  }
--this is tabelB, at the end it should contain the times of all four trains. The key is for defining and the value should be the corresponding train.
tabelB = {
			 [1] = " "
			 [2] = " "
			 [3] = " "
			 [4] = " "
			 }
-- so as an example of an correctly filled tabel:
[1] = "train1"
[2] = "train3"
[3] = "train2"
[4] = "train4"
cant figure out how to sort these.
any help is appreciated ;)/>

by my understanding, your table will never sort and be abled to output your desired results.

becase your keys are what you want to change the order of.

you would have to store the table as fx. tableA[1] = "train1", 60 … This way you maybe abled to run a sort on the 60…


I found this under a quick search… Then I played around a bit also and wrote this bit of code to make it work in CC….
local tableA = {
				[0] = {"train0", "20"},
				[1] = {"train1", "60"},
				[2] = {"train2", "40"},
				[3] = {"train3", "80"},
				[4] = {"train4", "20"},

				}

t = tableA
	
table.sort(t, function(a, B)/>   -- why does it add a /> here every time i edit...
	return tonumber(a[2]) > tonumber(b[2])
end)

for i = 1,#t do
  print(t[i][1])
end
the above code outputs …
Spoilertrain3
train1
train2
train4

this should return a check on the second value in a table to see if e1 is bigger than e2 (the larger value goes first when sorted).

(I had troubles with getting it to work at first with attempt to compare nil and nil…) that was untill i realised I had forgotten to tonumber the text numbers.


this is the example table I found… just to give the full example.
Spoiler

table = {
	[1] = {"something", "high"}
	[2] = {"something else", "low"}
	[3] = {"something further", "medium"},
	[4] = {"yet more something", "medium"},
}
priorities = {high = 2, medium = 1, low = 0}
table.sort(t, function(e1, e2)
	return priorities[e1[2]] > priorities[e2[2]]
end)

to be a bit more fancy… you could make a data entry for the train and times…
eg ask on screen "train no : " then get input of a number as "trnr" . then ask for time as "trti".

use
trnr = "train" .. trnr – this makes out number into a string that says for example "train5" if we entered 5

– could insert a table check here to see if train5 exists in the table already

table.insert(tableA, newdata(trnr, trti))

have a function that inserts the data into a table value.

function newdata(nr, ti) -- insert data
  local u = {}
  u[1] = nr
  u[2] = ti
  return u
end

this then stores our new data in the next slot. and voila an updateable table that adds new entries. (entering the same train number is not checked here…
mlewelt #15
Posted 13 July 2013 - 12:39 PM
wow thats a very detailed answer! will try it with this, should be working.
Big thanks man!