[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Sorting cogs into different pegs
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Sorting cogs into different pegs

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Alderkar   (11 posts)  [Biography] bio
Date Mon 13 Mar 2017 02:53 AM (UTC)

Amended on Mon 13 Mar 2017 11:01 PM (UTC) by Nick Gammon

Message
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
[Go to top] top

Posted by Alderkar   (11 posts)  [Biography] bio
Date Reply #1 on Mon 13 Mar 2017 03:37 AM (UTC)
Message
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 :) :) :)
[Go to top] top

Posted by Alderkar   (11 posts)  [Biography] bio
Date Reply #2 on Mon 13 Mar 2017 04:37 AM (UTC)
Message
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")
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Mon 13 Mar 2017 11:03 PM (UTC)
Message
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")

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Alderkar   (11 posts)  [Biography] bio
Date Reply #4 on Tue 14 Mar 2017 03:31 PM (UTC)
Message
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.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


1,478 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]