Sorting cogs into different pegs

Posted by Alderkar on Mon 13 Mar 2017 02:53 AM — 5 posts, 21,410 views.

#0
I am just learning and have been banging my head against this for a bit and was hoping for some more experienced eyes. Im very inexperienced and i need a explain it like im 5...


what im trying to do is sort circles into pegs for a mini game. I have compiled all the combinations that are possible (120) into a text file and now im trying to put those numbers into a table so that it trys it, then loops back and trys the next line in the file. So far i have this...


file = io.open("file.txt", "r")

for line in file:lines() do
print(line)   
--This works and prints the output:
--     1,2,3,4,5
--     5,4,3,2,1 
end

     table = {1,2,3,4,5}


for key , value in pairs(table) do 
--print ("cog",key,"peg",value)
Send("put ","cog",key," ","peg",value)

end




--This works and sends

put cog1 peg1
put cog2 peg2
put cog3 peg3
put cog4 peg4
put cog5 peg5






Now im having a hard time instead of explicitly telling it to set the table like this

" table = {1,2,3,4,5}"

i want it to do something like this

" table = {line data from the file read above}"



and then loop it back through each possibility from a file of 120 lines which has data that looks like this


1,2,3,4,5
1,2,3,5,4
1,2,4,3,5
1,2,4,5,3
1,2,5,3,4
1,2,5,4,3
1,3,2,4,5
1,3,2,5,4...


all the way through all the combinations.

Each time i try to do that im not getting the output im expecting. I am banging my head against the wall the right way to do it.

I really appreciate all you advanced level guys helping out the newbs like me
Amended on Mon 13 Mar 2017 11:01 PM by Nick Gammon
#1
I actually might know what im doing wrong. For the dudes like me who are learning

when changing just to

table = {line}
output im getting is:

cog 1 peg 1,2,3,4,5
cog 1 peg 5,4,3,2,1

so its just overwriting my key with a different value (1,2,3,4,5, and 5,4,3,2,1)

if I go "print(type(line))"
it tells me the type of my data is a string.

Which is probably why the table is not grabbing the data in the way I want it. Still need help trying to find out how to import this as numbers and not as a string if thats possible :) :) :)
#2
alright. Here is my solution so far.

Sorry for the message board spam

i have changed my data so its more like
12345
54321 (etc)

I figured from google that i had to grab the number and split it up into different variables so this is how i did it. im playing with mushclient because for some reason i get the right output on sublime(editor im using) but mushclient wont send the data. I might have a different version of lua? im not sure...


file = io.open("thenumbers.txt", "r")

for line in file:lines() do
s=line
--print("printing line", line)

c1,c2,c3,c4,c5=s:match('(%d)(%d)(%d)(%d)(%d)')
t={tonumber(c1),tonumber(c2),tonumber(c3),tonumber(c4),tonumber(c5)}

table = {t[1],t[2],t[3],t[4],t[5]}

for key , value in pairs(table) do
print("cog",key,"peg",value)
Send("put ","cog",key," ","peg",value)
end
end
Send("test")
Send("get all peg1 and peg2 and peg3 and peg4 and peg5")
Australia Forum Administrator #3
Template:codetag
To make your code more readable please use [code] tags as described here.


I've done your first post, please fix up the others. It's hard to read what you are trying to do.

Don't call a variable "table" because that is the name of a table inside Lua.

For example, if you assign something to "table" then you won't be able to do things like:


table.insert (foo, "hello")
#4
oops lol

thanks for getting back to me, i ended up getting this working but really really not in the way it should... i basically got everything to spit out the commands i need into a text file and then i made that text file a lua script and call that.

ill clean up/show my crappy solution soon for anyone searching this topic later.