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 ➜ converting 1,000,000 to 1000000

converting 1,000,000 to 1000000

Posting of new messages is disabled at present.

Refresh page


Posted by Norbert   USA  (61 posts)  Bio
Date Sat 08 Apr 2006 02:58 PM (UTC)
Message
I'm trying to change my scripts over to Lua from VBscript and I'm running into some complications. I keep getting an error saying that I'm trying to do math on a nil value.



function conv(number)
   a = string.gsub(number, ",", "")
   return a
end

function DoInfobar ()
   CurGLV = tonumber(GetVariable("CurGLV"))
   CurELV = tonumber(GetVariable("CurELV"))
   HPcur  = tonumber(GetVariable("HPcur"))
   HPmax  = tonumber(GetVariable("HPmax"))
   CurEScore = tonumber(conv(GetVariable("CurEScore")))
   CurPXP = string.gsub(GetVariable ("CurPXP"),",","")
   CurGXP = string.gsub(GetVariable("CurGXP"),",","")
   kills  = tonumber(GetVariable("worthies"))
   Money  = tonumber(GetVariable ("Money"))
   Scales = GetVariable("Scales")
   Hunger = GetVariable("Hunger")
   CurAlign = GetVariable("CurAlign")
   Totalneeded = 100
   a = 2
   b = 500000
   for i = 1, 37, 1 do
      b = (a * 250000) + b
      a = a + (i - 1)
      c = (a * 250000)
      if i == CurELV then
         Elastlv = b
         ETotalneeded = c
         break
      end --If
   end -- for

   CurEScore = tonumber(CurEScore) - tonumber(ELastlv)

   alignments = {
      W = { str = "white lord", color = "red" },
      P = { str = "paladin", color = "red" },
      C = { str = "crusader", color = "gold" },
      G = { str = "good", color = "green" },
      H = { str = "honorable", color = "green" },
      N = { str = "neutral", color = "lime" },
      M = { str = "malicious", color = "green" },
      E = { str = "evil", color = "green" },
      I = { str = "infamous", color = "gold" },
      B = { str = "black knight", color = "red" },
      L = { str = "lord of evil", color = "red" },
   }

   table.foreach (alignments,
      function (k, v)
         if  alignments.str == CurAlign then
            InfoColour (alignments.color)
         end --if
      end -- function
   )

   InfoClear ()
   InfoBackground ("white")
   InfoFont ("Arial", 12, 1)
   InfoColour ("purple")
   Info (GetInfo (2))
   InfoColour ("black")
   Info (" Dragon Grade: ")
   InfoColour ("blue")
   Info (CurGLV)
   InfoColour ("blue")
   Info ("  E")
   InfoColour ("blue")
   Info (CurELV)
   DoGauge("  HP: ", HPcur, HPmax, "darkgreen", "red")
   DoGauge("  GXP: ", CurGXP, Totalneeded, "blue", "blue")
   DoGauge("  EXP: ", CurEScore, ETotalneeded, "blue", "blue")
   InfoColour ("black")
   Info (" ALIGN: ")
   Info (CurAlign)
   InfoColour ("black")
   Info (" Kills: ")
   InfoColour ("green")
   Info (kills)
end -- doinfobar



If someone could help me out here it would be much appreciated.

Norbert

-Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens
It's a dumb question... skip it.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Sat 08 Apr 2006 11:57 PM (UTC)
Message
Do you have an indication of where the error is occurring? It looks like there are a lot of places where it could be, so if you had a line number that would help tracking things down.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

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

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #2 on Sun 09 Apr 2006 12:46 AM (UTC)
Message
Do all those variables exist? Maybe allow for the GetVariable returning nil:


function conv(number)
  return (string.gsub(number or 0, ",", ""))
end



The "or 0" there will substitute zero if the number is nil.

- Nick Gammon

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

Posted by Norbert   USA  (61 posts)  Bio
Date Reply #3 on Sun 09 Apr 2006 04:26 PM (UTC)

Amended on Sun 09 Apr 2006 05:03 PM (UTC) by Norbert

Message
The error is in

CurEScore = tonumber(CurEScore) - tonumber(ELastlv)

and this is the error message.

Error number: 0
Event: Run-time error
Description: [string "Script file"]:58: attempt to perform arithmetic on a nil value
stack traceback:
[string "Script file"]:58: in function `DoInfobar'
Called by: Function/Sub: GuildVar called by trigger
Reason: processing trigger ""

I added in the 'or 0' but I keep getting the same error. And all the variables are present in the world configuration window.

Also when I do
print(string.gsub("100,000",",",""))

It prints 100000 1 where the 1 is the number of substitutions done on the string.
but when I do
a = string.gsub("100,000",",","")
print (a) 

It prints only 100000 what happen to the number of substitutions where did that go? Is it messing up my computations.
Here are the variables and their values that I'm working with.

<variables>
  <variable name="CurAlign">neutral</variable>
  <variable name="CurELV">12</variable>
  <variable name="CurEScore">74,352,090</variable>
  <variable name="CurGLV">18</variable>
  <variable name="CurGXP">64</variable>
  <variable name="CurPXP">79,308,981</variable>
  <variable name="HPcur">286</variable>
  <variable name="HPmax">311</variable>
  <variable name="Hunger">not hungry</variable>
  <variable name="Money">551109</variable>
  <variable name="Power">excellent</variable>
  <variable name="Scales">excellent</variable>
  <variable name="worthies">10789</variable>
</variables>


Norbert

-Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens
It's a dumb question... skip it.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #4 on Sun 09 Apr 2006 09:06 PM (UTC)
Message
Quote:
Also when I do

print(string.gsub("100,000",",",""))


It prints 100000 1 where the 1 is the number of substitutions done on the string.
but when I do
a = string.gsub("100,000",",","")
print (a)


It prints only 100000 what happen to the number of substitutions where did that go?

string.gsub returns multiple values. The first is the result of the substitution, the other is the number of substitutions made.

When you call print, it prints out all values, which is why you get the 1. (Technically, it's printing out a list of sorts, which is, more or less, what string.gsub is giving you.) But when you say a = string.gsub, that's only taking the first return value and is throwing away the rest.

If you want to keep the second return value, just write something like:
a,b = string.gsub("100,000", "," , "")
print(a, b)



As to your other problem, ELastlv is not in your list of variables. Try writing:
CurEScore = tonumber(CurEScore) - (tonumber(ELastlv) or 0)

If that doesn't work, replace ELastlv with: ELastlv or "0".

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

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

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #5 on Sun 09 Apr 2006 09:10 PM (UTC)
Message
I was about to say all that! Plus, to discard the extra values from string.gsub, put the call in parentheses, like this:

print((string.gsub("100,000",",",""))) --> 100000

If you look carefully, you will see I did that in my suggested amendment to your "conv" subroutine:


function conv(number)
  return (string.gsub(number or 0, ",", ""))
end


The brackets around the returned value means it will simply return the converted number and not the number of replacements.

- Nick Gammon

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

Posted by Norbert   USA  (61 posts)  Bio
Date Reply #6 on Mon 10 Apr 2006 03:37 PM (UTC)
Message
I got it fixed and it was a silly mistake. I had a typo in the variable name I had ELastlv instead of Elastlv. Thanks for your help and sorry for wasting your time.

Norbert

-Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens
It's a dumb question... skip it.
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.


17,775 views.

Posting of new messages is disabled at present.

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.