[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]  Useful calculator Alias
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Useful calculator Alias

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


Pages: 1 2  3  

Posted by Henry Tanner   Finland  (23 posts)  [Biography] bio
Date Wed 18 Aug 2010 02:12 PM (UTC)
Message
I have an alias "math *",
which sends:
result = %1
Note ("= " .. result)

to script.
I find myself using this one daily, I hope you can find an use for it :)

I got this!
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #1 on Wed 18 Aug 2010 05:32 PM (UTC)
Message
Definitely a useful alias! I use something slightly different (and perhaps a little more flexible):

<aliases>
<alias
  match="^=(.*)$"
  regexp="y"
  send_to="12"
  enabled="y'
  ignore_case="y">
  <send>print(assert(loadstring("return %1"))())</send>
</alias>
</aliases>


(I might've messed up the XML a little since I wrote that out by hand.)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #2 on Wed 18 Aug 2010 07:05 PM (UTC)
Message
Why's it more flexible? They seem to both be evaluating an expression and printing it.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Wed 18 Aug 2010 09:10 PM (UTC)
Message
I don't think the loadstring gets you much further down the track, but this variation saves the assignment line:


<aliases>
  <alias
   match="math *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>print ("=", %1)</send>
  </alias>
</aliases>



- Nick Gammon

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Wed 18 Aug 2010 09:35 PM (UTC)

Amended on Wed 18 Aug 2010 09:38 PM (UTC) by Nick Gammon

Message
The only problem with that (and the original) is that if you want to do maths-type things like sqrt you have to do math.sqrt or you see something like this:



math sqrt (9)

Run-time error
World: SmaugFUSS
Immediate execution
[string "Alias: "]:1: attempt to call global 'sqrt' (a nil value)
stack traceback:
        [string "Alias: "]:1: in main chunk


This rather whimsical alias below solves that by letting you use anything in the math table without having to put "math" in front of it. It's all done in a single "line" (one function call) although I added a couple of linebreaks for readability. I'll leave it as an exercise to work out how it works. Suffice to say that it uses a metatable, and calling a function under a different environment.


<aliases>
  <alias
   match="math *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
print ("=", 
       setfenv (function () return %1 end, 
       setmetatable ( {}, { __index = function (t, n) return math [n] end })) ())
</send>
  </alias>
</aliases>


Template:pasting For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.


Testing:


math 45847/3433           --> = 13.354791727352
math abs (-435938+23432)  --> = 412506
math log (2343)           --> = 7.7591874385078
math 45847*4532           --> = 207778604
math sqrt (128)           --> = 11.313708498985


- Nick Gammon

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Wed 18 Aug 2010 10:25 PM (UTC)
Message
Another variation lets you do more stuff (like the string library, the bit library, the utils library) like this:


<aliases>
  <alias
   match="= *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
print ("=", 
       setfenv (function () return %1 end, 
       setmetatable ( {}, { __index = function (t, n) 
         return math [n] or bit [n] or string [n] or utils [n] end })) ())
</send>
  </alias>
</aliases>


This one shortens the alias to "=" to save typing, eg.


= hash ('nick')  --> = 2609427bf1f8b42def2bab7d01da5a4fa56db539
= find ('nick cooks', '(co+)')  --> = 6 8 coo
= log (3431234)  --> = 15.048430521205


Notice that, as in the "find" example, you can get multiple results (the columns and the matching text).

Also notice that if you want to use strings you must put them in single quotes because MUSHclient "helpfully" puts a backslash in front of double quotes.

- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #6 on Wed 18 Aug 2010 11:09 PM (UTC)
Message
What I did lets you use "", since it's inserted into the loadstring argument.

I never thought about shortcut aliases for namespaces like that, Nick. Very interesting! I just use the general "=math.floor(1.5)" because it mimics the Lua REPL's shorthand.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Thu 19 Aug 2010 12:08 AM (UTC)

Amended on Thu 19 Aug 2010 12:09 AM (UTC) by Nick Gammon

Message
To use loadstring in my method (removing the need for math.xxx) is fiddlier because the loadstring environment fails. I tried it. ;)

This slightly amended version echoes the original string:


<aliases>
  <alias
   match="=*"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
print ("%1 =", 
       setfenv (function () return %1 end, 
       setmetatable ( {}, { __index = function (t, n) 
         return math [n] or bit [n] or string [n] or utils [n] end })) ())
</send>
  </alias>
</aliases>


So you see the "question" next to the "answer" which can be helpful if you are doing a lot of maths. You might wonder which answer refers to which question.

eg.


=5*8        --> 5*8 = 40
=log10(500) --> log10(500) = 2.698970004336
= sqrt (2)  -->  sqrt (2) = 1.4142135623731



- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #8 on Thu 19 Aug 2010 12:24 AM (UTC)

Amended on Thu 19 Aug 2010 12:26 AM (UTC) by Twisol

Message
Hrm, this worked for me:

<aliases>
  <alias
   match="=*"
   enabled="y"
   send_to="12"
   sequence="100"
  >
    <send>
local env = setmetatable({}, {
  __index = function(t, key)
    return math[key] or bit[key] or string[key] or utils[key]
  end,
})
print("%1 =", setfenv(assert(loadstring("return %1"), env))())
    </send>
  </alias>
</aliases>


Test:

=split("4,3,2", ",")
split("4,3,2", ",") = table: 010426E0

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #9 on Thu 19 Aug 2010 03:46 AM (UTC)

Amended on Thu 19 Aug 2010 06:20 AM (UTC) by Nick Gammon

Message
You have a bracket in the wrong spot. If you submit an erroneous expression you get:


=a+

Run-time error
World: SmaugFUSS
Immediate execution
[string "Alias: "]:7: bad argument #2 to 'assert' (string expected, got table)
stack traceback:
        [C]: in function 'assert'
        [string "Alias: "]:7: in main chunk



This version fixes that:


<aliases>
  <alias
   match="=*"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
local env = setmetatable({}, {
  __index = function(t, key)
    return math[key] or bit[key] or string[key] or utils[key]
  end,
})
print("%1 =", setfenv(assert(loadstring("return %1")), env)())
    </send>
  </alias>
</aliases>


Now testing:


=a+

Run-time error
World: SmaugFUSS
Immediate execution
[string "Alias: "]:7: [string "return a+"]:1: unexpected symbol near '<eof>'
stack traceback:
        [C]: in function 'assert'
        [string "Alias: "]:7: in main chunk



And in the spirit of trying to make it as obscure as possible, here is my version without the intermediate variable:


<aliases>
  <alias
   match="=*"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
print ("%1 =", setfenv (assert (loadstring "return %1"), setmetatable ({}, 
    {__index = function (_, n) return math [n] or bit [n] or string [n] or utils [n] or world [n] end}) ) () )
</send>
  </alias>
</aliases>



Oh, and thanks to Henry Tanner for kicking off this rather interesting thread. :)

[EDIT] Modified last one to make it shorter. And to add the "world" table.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Thu 19 Aug 2010 03:51 AM (UTC)

Amended on Thu 19 Aug 2010 03:53 AM (UTC) by Nick Gammon

Message
Speaking of obscure code:

http://www0.us.ioccc.org/main.html

There have been some very good entries in that over the years. For example, an adventure game where you got a whole lot of messages, none of which were visible in the source, at all (I think they were encoded as a variable number of spaces, or something very silly like that).


http://en.wikipedia.org/wiki/International_Obfuscated_C_Code_Contest

- Nick Gammon

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

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #11 on Thu 19 Aug 2010 04:16 AM (UTC)
Message
Quote:
What I did lets you use "", since it's inserted into the loadstring argument.

Hmm? Well, beyond the fact that I'm not sure how that's useful, how does the original version not allow that? When you say "", do you mean just doing:

> math <enter>

meaning to print out the empty string to the world?

or do you mean

> math ""<enter>


Nick: nice trick with the metatable. :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #12 on Thu 19 Aug 2010 04:45 AM (UTC)

Amended on Thu 19 Aug 2010 05:27 AM (UTC) by Nick Gammon

Message
Thanks David!

On the very original, if you do this:


math string.match ("nick", "i")


You get:


Compile error
World: SmaugFUSS
Immediate execution
[string "Alias: "]:1: unexpected symbol near '\'


And similar for the variants that don't use loadstring.

This is because MUSHclient notices the quotes and tries to fix them by putting \ in front of them, so the parser sees:


math string.match (\"nick\", \"i\")


It assumes that you would want to put %1 inside quotes, and therefore quotes inside %1 need to be escaped.

The loadstring works around this, because you *have* put %1 inside quotes.

Mind you, the original one is still the simplest by far. The only problems are that one (the quotes) and the fact that things like sqrt have to be math.sqrt.

- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #13 on Thu 19 Aug 2010 05:32 AM (UTC)

Amended on Thu 19 Aug 2010 05:33 AM (UTC) by Twisol

Message
Nick Gammon said:
You have a bracket in the wrong spot.

Whoops! I got a table for my result and didn't bother to check what it was, since I was using split() to test.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by WillFa   USA  (525 posts)  [Biography] bio
Date Reply #14 on Thu 19 Aug 2010 05:54 AM (UTC)
Message
Nick Gammon said:

Thanks David!

On the very original, if you do this:


math string.match ("nick", "i")


You get:


Compile error
World: SmaugFUSS
Immediate execution
[string "Alias: "]:1: unexpected symbol near '\'

...


Why would you want to math a string.find? This is a quick calculator, not a replacement for /print() after all.
[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.


20,727 views.

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

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]