converting 1,000,000 to 1000000

Posted by Norbert on Sat 08 Apr 2006 02:58 PM — 7 posts, 25,375 views.

USA #0
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.
USA #1
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.
Australia Forum Administrator #2
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.
USA #3
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>

Amended on Sun 09 Apr 2006 05:03 PM by Norbert
USA #4
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".
Australia Forum Administrator #5
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.
USA #6
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.