This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
boudragon's profile picture

tostring, or not tostring?

Started by boudragon, 07 July 2013 - 04:51 PM
boudragon #1
Posted 07 July 2013 - 06:51 PM
Ok so I wasn't sure what exactly to search for in the forums… apologies in advance if this exists but I've been at this for hours now. Thing is it's SO simple… yet I can't seem to see the problem. Here is an example of what I am trying to do:

rnd = (math.random (100,500))
apass = ((rnd + 11) * 83)
ovrpass = read()

if ovrpass == apass then
print("Override Success…")
sleep(3)
else
print("Override Failure…")
end

I have tried adding a tostring to the rnd variable, apass and both or leaving them as they are above but get NOWHERE. So frustrating! Please help! :)/>
MR_nesquick #2
Posted 07 July 2013 - 07:02 PM
local rnd = math.random(100,500)
local apass = (rnd+11) * 83


if tonumber(ovrpass) == apass then


–edit
if you are going to use apass more then one time i would do it like MysticT, but if you are only using apass one time it doesn't really matte.
MysticT #3
Posted 07 July 2013 - 07:03 PM
You can either tostring apass, or tonumber ovrpass. In this case, I would use tostring:

local rnd = math.random(100, 500)
local apass = tostring((rnd + 11) * 83)
local ovrpass = read()

if ovrpass == apass then
  print("Override Success...")
else
  print("Override Failure...")
end
boudragon #4
Posted 07 July 2013 - 07:08 PM
Ahhh but here is the issue… when I tostring the apass… it fails… program crashes and says attempt to call nil.
boudragon #5
Posted 07 July 2013 - 07:25 PM
FOUND IT… I'm slow today guys hang tight LMAO! Turns out on my line 152 I have a term.clearLine() …. I HAD just clearLine() thanx for the advice though!
AgentE382 #6
Posted 07 July 2013 - 07:29 PM
Simple solution: Replace `read()` with `io.read()`.

EDIT: Eh… I was a little late… Just watch out for similar errors. It seems you tend to forget the table prefix.
theoriginalbit #7
Posted 08 July 2013 - 01:31 AM
Simple solution: Replace `read()` with `io.read()`.

EDIT: Eh… I was a little late… Just watch out for similar errors. It seems you tend to forget the table prefix.
read is perfectly valid in CC Lua. It is a function added by the CC devs in `bios.lua`.
AgentE382 #8
Posted 08 July 2013 - 07:03 AM
Simple solution: Replace `read()` with `io.read()`.

EDIT: Eh… I was a little late… Just watch out for similar errors. It seems you tend to forget the table prefix.
read is perfectly valid in CC Lua. It is a function added by the CC devs in `bios.lua`.
Okay, thanks. I just don't have a PC on me, so no CC or CC-Desk to test the stuff I post. I'll check the wiki to see what other globals are in CC, so I don't make this mistake again.

I think the extra CC global functions may have encouraged forgetting the table prefix in this case. (What his actual problem turned out to be)
boudragon #9
Posted 08 July 2013 - 08:49 AM
AgentE382 is right… in most cases the prefix isn't necessary while others it is. Not necessarily confusing but there are times I do forget a prefix by assuming it's not needed.
theoriginalbit #10
Posted 08 July 2013 - 10:13 AM
I'm sorry, but I don't see where the confusion lies… Ok there are a few functions that don't exist inside of an API, 27 to be exact1, but here is a list of them, grouped into their commonality of usage2

Commonly used functions by the average user:
Spoiler
  • read — reads input from the user, unlike io.read will also allow for masking of characters for password inputs
  • write — writes text to to current term target, unlike term.write it allows special characters such as `\n` and it will perform line wrapping
  • print — uses the write function with a new line appended to the end (there is no alternative or similar function)
  • tostring — converts the supplied number, string, or table into a string (there is no alternative or similar function)
  • tonumber — converts the supplied string, into a number (there is no alternative or similar function)
  • error — will stop a program and print the supplied text to the terminal with the file name and line number it was called
  • sleep — will pause the program where it is for the supplied time. Note: os.sleep actually uses this function, so it is better to use sleep than os.sleep
Lets pause here for a second, that is exactly 7 functions that the average user has to remember don't get called on an API, those numbers are something that can be done by short term memory! Anyway, lets continue…

List of the functions used by most of the people on these forums that write 'advanced' programs (by advanced I don't mean for the advanced computer, I mean programatic complexity):
Spoiler
  • assert
  • type
  • loadstring
  • unpack
  • pcall
  • pairs
  • ipairs

Functions used by the very advanced programs:
Spoiler
  • getfenv
  • setfenv
  • setmetatable
  • getmetatable
  • rawset
  • rawget

Rarely used functions:
Spoiler
  • printError
  • next
  • select
  • loadfile
  • dofile

Ones I've never seen anyone use:
Spoiler
  • xpcall — pretty sure it's because iirc and afaik this is redundant in CCLua
  • rawequal

1.List was compiled by memory and confirmed by running the following code `for k,v in pairs(_G) do print(k) os.pullEvent('char') end` and checking the names that don't correspond to an API
2. All these groupings a based off what I have seen on these forums over the past several months
Edited on 08 July 2013 - 08:15 AM
Apfeldstrudel #11
Posted 08 July 2013 - 10:22 AM
Did you misscapitalize tostring?
Edit: title sounds like skakespeare
boudragon #12
Posted 08 July 2013 - 12:09 PM
theoriginalbit it wasn't a matter of what existed within the API… the problem I had originally was what I THOUGHT a tostring or tonumber issue cause I kept getting a attempt to call nil. Turned out I forgot the "term." in front of a clearLine(). Simple mistake on my part cause I'm use to using simplified commands like write("") instead of term.write(""). See what I mean?

Xyexs - Yes… I was doing a Shakespeare reference LMAO!
AgentE382 #13
Posted 08 July 2013 - 12:52 PM
I'm sorry, but I don't see where the confusion lies… Ok there are a few functions that don't exist inside of an API, 27 to be exact1, but here is a list of them, grouped into their commonality of usage2

Commonly used functions by the average user:
Spoiler
  • read — reads input from the user, unlike io.read will also allow for masking of characters for password inputs
  • write — writes text to to current term target, unlike term.write it allows special characters such as `\n` and it will perform line wrapping
  • print — uses the write function with a new line appended to the end (there is no alternative or similar function)
  • tostring — converts the supplied number, string, or table into a string (there is no alternative or similar function)
  • tonumber — converts the supplied string, into a number (there is no alternative or similar function)
  • error — will stop a program and print the supplied text to the terminal with the file name and line number it was called
  • sleep — will pause the program where it is for the supplied time. Note: os.sleep actually uses this function, so it is better to use sleep than os.sleep
Lets pause here for a second, that is exactly 7 functions that the average user has to remember don't get called on an API, those numbers are something that can be done by short term memory! Anyway, lets continue…

List of the functions used by most of the people on these forums that write 'advanced' programs (by advanced I don't mean for the advanced computer, I mean programatic complexity):
Spoiler
  • assert
  • type
  • loadstring
  • unpack
  • pcall
  • pairs
  • ipairs

Functions used by the very advanced programs:
Spoiler
  • getfenv
  • setfenv
  • setmetatable
  • getmetatable
  • rawset
  • rawget

Rarely used functions:
Spoiler
  • printError
  • next
  • select
  • loadfile
  • dofile

Ones I've never seen anyone use:
Spoiler
  • xpcall — pretty sure it's because iirc and afaik this is redundant in CCLua
  • rawequal

1.List was compiled by memory and confirmed by running the following code `for k,v in pairs(_G) do print(k) os.pullEvent('char') end` and checking the names that don't correspond to an API
2. All these groupings a based off what I have seen on these forums over the past several months
There are actually only 4 non-standard global functions in your list:
  1. read
  2. write
  3. sleep
  4. printError
The only problem is that the first 3 are some of the most commonly used functions. Often, they're the most integral functions in users' first programs, so people get used to calling functions without a table.
boudragon #14
Posted 08 July 2013 - 12:59 PM
Exactly. It's just one of those easy to forget things… no biggy :)/>
diegodan1893 #15
Posted 08 July 2013 - 01:41 PM
tostring, or not tostring, that is the question…