Problems with tonumber(e) - Printable Version +- Logic Machine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Problems with tonumber(e) (/showthread.php?tid=2564) |
Problems with tonumber(e) - mjaanes - 03.04.2020 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." RE: Problems with tonumber(e) - Daniel - 03.04.2020 What is saved in the storage? RE: Problems with tonumber(e) - admin - 03.04.2020 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) RE: Problems with tonumber(e) - mjaanes - 03.04.2020 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 |