Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Assistance with a little project

Assistance with a little project

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


Pages: 1  2 

Posted by Shinrar Vallar   (13 posts)  Bio
Date Reply #15 on Mon 25 Feb 2008 07:00 PM (UTC)

Amended on Mon 25 Feb 2008 07:02 PM (UTC) by Shinrar Vallar

Message
Gah!

My money is on that being it. My spelling stinks, and it doesn't help that my attention is split. Thanks... Now I feel like a fool.

Edit:
Yep. That fixed it. Bah. I need install TextPad at work to spellcheck for me, I think.
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #16 on Tue 26 Feb 2008 04:47 AM (UTC)
Message
A spellchecker is not really going to help. For example:


foo = 5
bar = fooo + 1


I don't see how a spell checker will detect the extra o in the second "fooo". You just have to pay attention to detail, particularly when things behave unexpectedly.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #17 on Tue 26 Feb 2008 06:26 PM (UTC)
Message
Lua comes with the 'strict' module that can detect accesses to undeclared variables from inside a function. It's a little finnicky sometimes but has saved me from many an error.

The following, for instance, is an error:

foo = 123

function f()
return fooo + 1 --> error, undeclared "fooo"
end


It works by messing with the global table's metatable and intercepting accesses to it, making sure they're for variables that were used before at the global level.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Shinrar Vallar   (13 posts)  Bio
Date Reply #18 on Wed 27 Feb 2008 05:44 PM (UTC)

Amended on Wed 27 Feb 2008 05:46 PM (UTC) by Shinrar Vallar

Message
Everyone has been a great help!

I've got it working enough that it runs as I tweak it on the fly, constantly reloading it. I'm now slowly moving over all of my 'by hand' modifications (setting variables via aliases) to automated ones that pull the data from the Mud. Or essentially - getting the script to run 100% by itself, with the exception of a command for: Start, Stop, Abort, and Resume (Start gets everything going. Stop ends it after the next go around. Abort stops it right then and there. Resume continues from where abort left off). I've got all but Resume working already, but thats not what my next problem is...

I'm still having problems pulling the data from the Sorting-script out, and placing that data into variables which can be access and evaluated by the script. That is:
-- calculate a to b

for k, v in pairs (tbl) do
  v.atob = (v.b - v.a) / v.c  -- difference divided by a constant
end -- for loop


-- display results

require "tprint"
tprint (tbl)


-- sort to find highest atob

require "pairsbykeys"

function f (ka, kb)
  return tbl [ka].atob > tbl [kb].atob
end -- sort comparison function

for k, v in pairsByKeys (tbl, f) do
  print ("-----", k, "-----")
  tprint (v)
end -- for
The above gives me a new value in my table, named 'atob', and sorts it so that the appropriate value appears at the bottom/top.

What I need is some way pull the name of the table with the highest atob value (IE, if tbl.locationA_resourceB.atob has the highest value, I need to store "tbl.locationA_resourceB" into said variable). It would be extra nice if I could store the bits on either side of the _ into separate variables, but I can at least (and what I'm planning on doing) is throw a mess of ifchecks at it, and assign the two needed values that way.

David Haley gave me:
x,y = string.match(k, "Resource_(.)to(.)")
Which - modified for my script, should probably read:
x,y = string.match(k, "tbl.(.)_(.)")
Seeing as LocationA and ResourceA are actually represented by the names of the things. Or so I think. I could be wrong. But I don't see how the above could only grab the single one with the highest value - then again, I don't understand what string.match does exactly, so I could be just ignorant.

------------------------------------
Secondly - and hopefully much more simply:

I haven't figured out how to call a function from inside a function... Meaning - in order to call another script inside my script (subroutine?), I have to send a command to the mud that triggers a trigger which calls the second function. Its messy. I'm pretty sure that can be bypassed? Hopefully?
------------------------------------

Beyond that - I think its mostly finished!

I'd just like to extend a great thanks to everyone who's helped me make this possible. It means a lot to me.
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #19 on Wed 27 Feb 2008 07:40 PM (UTC)

Amended on Wed 27 Feb 2008 07:42 PM (UTC) by Nick Gammon

Message
Quote:

What I need is some way pull the name of the table with the highest atob value (IE, if tbl.locationA_resourceB.atob has the highest value, I need to store "tbl.locationA_resourceB" into said variable). It would be extra nice if I could store the bits on either side of the _ into separate variables, but I can at least (and what I'm planning on doing) is throw a mess of ifchecks at it, and assign the two needed values that way.


From your earlier code snippet, it seems to me you are doing things the hard way anyway, which is why you are having problems getting the data back. This is your code:


tbl = {
     Resource_AtoA = { a = 1.11, b = 1.11, c = 5 },
     Resource_BtoA = { a = 2.22, b = 1.11, c = 5 },
     Resource_CtoA = { a = 3.33, b = 1.11, c = 5 },
     Resource_AtoB = { a = 1.11, b = 9.99, c = 5 },
     Resource_BtoB = { a = 2.22, b = 1.11, c = 5 },
     Resource_CtoB = { a = 3.33, b = 1.11, c = 5 },
     Resource_AtoC = { a = 1.11, b = 1.11, c = 999 },
     Resource_BtoC = { a = 2.22, b = 2.22, c = 999 },
     Resource_CtoC = { a = 3.33, b = 3.33, c = 999 },
     }


Since you want to get out the "AtoA" part, why not put that into the table as a variable? In other words, the table would look like this instead:


tbl = {
  { from = "A", to = "A", a = 1.11, b = 1.11, c = 5 },
  { from = "A", to = "B", a = 1.11, b = 9.99, c = 5 },
  { from = "A", to = "C", a = 1.11, b = 1.11, c = 999 },
  { from = "B", to = "A", a = 2.22, b = 1.11, c = 5 },
  { from = "B", to = "B", a = 2.22, b = 1.11, c = 5 },
  { from = "B", to = "C", a = 2.22, b = 2.22, c = 999 },
  { from = "C", to = "A", a = 3.33, b = 1.11, c = 5 },
  { from = "C", to = "B", a = 3.33, b = 1.11, c = 5 },
  { from = "C", to = "C", a = 3.33, b = 3.33, c = 999 },
  }  -- end tbl


Notice each item does not have a name like, so Lua assigns numeric keys (ie. 1, 2, 3 etc.).

We can still calculate a to b value:



-- calculate a to b

for k, v in ipairs (tbl) do
  v.atob = (v.b - v.a) / v.c  -- difference divided by a constant
end -- for loop



Now to find the highest we simply sort the table:


-- sort table

table.sort (tbl, function (ka, kb) return ka.atob > kb.atob end)


I used a custom sort function to specify that the sort comparison was the "atob" value in each item.

The highest is simply the first item (subscript 1):


-- get highest atob value

print ("Highest atob is", tbl [1].atob, "from", tbl [1].from, "to", tbl [1].to)


My example printed:


Highest atob is 1.776 from A to B


Your problem now is putting new values into the table, as the table is not keyed by A, B, C etc. (they are inside the table but not keys). There are a couple of ways you can do that, and I might leave that as an exercise.

I suspect you are not totally comfortable with using tables, I would read up on Lua tables a bit and make a few examples for yourself. It is easier to do more complex code when you are familiar with doing easier code.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #20 on Wed 27 Feb 2008 08:23 PM (UTC)
Message
Quote:

I haven't figured out how to call a function from inside a function.


In what way have you not figured it out? For a start, see:

http://lua-users.org/wiki/FunctionsTutorial


Here is a simple example:


function add (a, b)

  return a + b

end -- add

function foo (x, y)

  print ("I will add x and y for you:")
  sum = add (x, y)  -- call function
  print (sum)

end -- foo

foo (22, 33)


Output:


I will add x and y for you:
55


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #21 on Thu 28 Feb 2008 08:36 PM (UTC)
Message
Quote:

Or essentially - getting the script to run 100% by itself, with the exception of a command for: Start, Stop, Abort, and Resume (Start gets everything going.


As Shaun Biggs pointed out in another post, many MUDs frown on bots, and this sounds like a bot if I ever saw one. You are planning to type "start", watch an hour of TV, and then type "stop"?

If one of the MUD admins notices you and starts asking you questions, which your bot will ignore, you might find your character deleted.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


79,199 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

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

Go to topic:           Search the forum


[Go to top] top

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