Jump to content




tostring, or not tostring?


  • You cannot reply to this topic
14 replies to this topic

#1 boudragon

  • Members
  • 107 posts

Posted 07 July 2013 - 05: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! :)

#2 MR_nesquick

  • Members
  • 106 posts
  • LocationNorway

Posted 07 July 2013 - 06: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.

#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 07 July 2013 - 06: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


#4 boudragon

  • Members
  • 107 posts

Posted 07 July 2013 - 06:08 PM

Ahhh but here is the issue... when I tostring the apass... it fails... program crashes and says attempt to call nil.

#5 boudragon

  • Members
  • 107 posts

Posted 07 July 2013 - 06: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!

#6 AgentE382

  • Members
  • 119 posts

Posted 07 July 2013 - 06: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.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 July 2013 - 12:31 AM

View PostAgentE382, on 07 July 2013 - 06:29 PM, said:

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`.

#8 AgentE382

  • Members
  • 119 posts

Posted 08 July 2013 - 06:03 AM

View Posttheoriginalbit, on 08 July 2013 - 12:31 AM, said:

View PostAgentE382, on 07 July 2013 - 06:29 PM, said:

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)

#9 boudragon

  • Members
  • 107 posts

Posted 08 July 2013 - 07: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.

#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 July 2013 - 09: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

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

Functions used by the very advanced programs:
Spoiler

Rarely used functions:
Spoiler

Ones I've never seen anyone use:
Spoiler

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 by theoriginalbit, 08 July 2013 - 09:15 AM.


#11 Apfeldstrudel

  • Members
  • 161 posts

Posted 08 July 2013 - 09:22 AM

Did you misscapitalize tostring?
Edit: title sounds like skakespeare

#12 boudragon

  • Members
  • 107 posts

Posted 08 July 2013 - 11:09 AM

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!

#13 AgentE382

  • Members
  • 119 posts

Posted 08 July 2013 - 11:52 AM

View Posttheoriginalbit, on 08 July 2013 - 09:13 AM, said:

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

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

Functions used by the very advanced programs:
Spoiler

Rarely used functions:
Spoiler

Ones I've never seen anyone use:
Spoiler

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:
  • read
  • write
  • sleep
  • 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.

#14 boudragon

  • Members
  • 107 posts

Posted 08 July 2013 - 11:59 AM

Exactly. It's just one of those easy to forget things... no biggy :)

#15 diegodan1893

  • Members
  • 164 posts
  • LocationSpain

Posted 08 July 2013 - 12:41 PM

tostring, or not tostring, that is the question...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users