This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Problems with tonumber(e)
#1
I have the following super simple code.
------------------------------------
log('AAAAAAA')
sUID = 'dtCSV_UID' .. '_' .. sCurrTag .. 'x'
log('BBBBBBB: ' .. sUID)
iLastTime = tonumber(storage.get(sUID, 0))
log('CCCCCCC')


The code never gets to point 'CCCCCC'. 

It produces the following output in the log:
* string: AAAAAAA
* string: BBBBBBB: dtCSV_UID_HEATING_CTRL_VARx
(and nothing more)

The tonumber function call is really not needed and is meant to be a safeguard to force the value from storage.get into a number.
If I remove the "tonumber" and just use the simpler form:
iLastTime = storage.get(sUID, 0)

Then everything works fine.

The error log shows the following:
User script:34: bad argument #2 to 'tonumber' (number expected, got string)
stack traceback: [C]: in function 'tonumber' User script:34: in main chunk


Why is this? 
Isn't the whole point of tonumber to convert a number or string to a number?

From the manual (on the tonumber function):
"Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns this number; otherwise, it returns nil."
Reply
#2
What is saved in the storage?
------------------------------
Ctrl+F5
Reply
#3
This happens because storage.get returns more than 1 value.

Either add another set of parentheses:
Code:
iLastTime = tonumber((storage.get(sUID, 0)))

Or split it into two lines to make it more readable:
Code:
value = storage.get(sUID, 0)
iLastTime = tonumber(value)
Reply
#4
Thanks for quick and interesting answers.
Every piece of documentation I have read indicates that storage.get returns one value.
Using log, I know see that it returns nil + an additional explanatory message when the storage.get does not exist.
Thanks so much for clarifying.
Rgds
Christian
Reply


Forum Jump: