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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Convert_Words_to_Numbers Comparing Userdata Result

Convert_Words_to_Numbers Comparing Userdata Result

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


Posted by Xvordan   (29 posts)  Bio
Date Tue 14 May 2019 10:42 AM (UTC)
Message
I'm trying to use the words_to_numbers.lua functions to convert a word into a number, but the function returns a result that's of a userdata type. I can't use this variable type in if statements and other number comparisons. Is there an easy way to convert this variable type to a proper integer? The tonumbers() function apparently only works on strings.
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Tue 14 May 2019 11:45 AM (UTC)

Amended on Tue 14 May 2019 11:46 AM (UTC) by Fiendish

Message
Please show what you're doing.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Xvordan   (29 posts)  Bio
Date Reply #2 on Tue 14 May 2019 12:23 PM (UTC)

Amended on Tue 14 May 2019 12:24 PM (UTC) by Xvordan

Message
require "words_to_numbers"
x = convert_words_to_numbers("one thousand three hundred and eighty four")
Note(x) -- 1384
Note(tonumber(x)) -- nil
if x > 1000 then
Note("x is bigger than 1000.")
end

Run-time error
World: Godwars2
Immediate execution
[string "Immediate"]:3: attempt to compare number with userdata
stack traceback:
[string "Immediate"]:3: in main chunk

I'd just like to convert x to an integer value that I can use in if statements against another number.
Top

Posted by Xvordan   (29 posts)  Bio
Date Reply #3 on Tue 14 May 2019 12:53 PM (UTC)
Message
Found the solution to my own problem. I'd forgotten that I had written some scripts to convert huge numbers into more easily digestable versions, and those scripts used bc.tonumber(). I has been a few years since I messed with the bc module, so forgot it could do that.
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #4 on Tue 14 May 2019 08:14 PM (UTC)

Amended on Tue 14 May 2019 08:18 PM (UTC) by Fiendish

Message
Interesting. I believe that this should be considered a bug in the function.

In the meantime, you can use

tonumber(tostring(convert_words_to_numbers("one thousand three hundred and eighty four")))

or

bc.tonumber(convert_words_to_numbers("one thousand three hundred and eighty four"))


instead of


convert_words_to_numbers("one thousand three hundred and eighty four")

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #5 on Wed 15 May 2019 03:01 AM (UTC)

Amended on Wed 15 May 2019 03:02 AM (UTC) by Nick Gammon

Message
The result from convert_words_to_numbers is necessarily a big number because it is designed to handle huge numbers.

All you have to do is run "tostring" on it to convert it back to a string (rather than userdata). Then you can run "tonumber" on that string, assuming it isn't too large.

For example:


require "words_to_numbers"
x = tonumber (tostring (convert_words_to_numbers("one thousand three hundred and eighty four")))
print (x)
if x > 1000 then
  Note("x is bigger than 1000.")
end


Alternatively:


require "words_to_numbers"
x = convert_words_to_numbers("one thousand three hundred and eighty four")
Note(x) -- 1384
x = tonumber (tostring (x))
Note(x) -- 1384
if x > 1000 then
   Note("x is bigger than 1000.")
end


The bignumber has an automatic "tostring" metatable entry on the userdata, so trying to print or Note it works (it converts it to a string under the hood). However comparisons don't call that as you can't compare numbers to strings.




This is pretty-much what Fiendish said, but I wanted to explain why it does that.

- 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.


14,633 views.

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.