Jump to content




checking if a variable exists


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

#1 etopsirhc

  • Members
  • 122 posts

Posted 06 March 2013 - 11:22 AM

currently i'm using
if var[varname] == nil then ...
but its giving me the attempt to call nil error , witch with what i'm doing i expect that variable not to exist yet. but i need to be able to make sure it doesn't exist before continuing on with the rest of the program.

simply put, how can i see if a var in a table exists when i expect it not to exist?

#2 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 06 March 2013 - 11:32 AM

Either your table does not exist or you are doing something wrong.

This works fine for me.
if variable[num] == nil then
--This is a fake!
else
--It's ok, this is real!
end


It would help if we had some code to work with ;)

EDIT: Technically the "var" is a table :P So, get rid of the above code :):

if table[var] == nil then
--IT'S A PHAT PHONY!
else
--We chill...
end


#3 etopsirhc

  • Members
  • 122 posts

Posted 06 March 2013 - 11:35 AM

what i'm doing is adding string based keys to a table when an event occures, but b4 that event it has to check to make sure that key doesnt alredy exist

http://pastebin.com/YGTZ8y3y

[edit i noticed something farther down thats wrong, but line 57 and 60 are the problematic ones]

#4 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 06 March 2013 - 11:41 AM

Ok what is "v"?

if v[event[2]] == nil and not confirmation then

EDIT:
confirmation = false
if v[event[2]] == nil and NOT? confirmation then

Are you saying if nil and not false?


#5 etopsirhc

  • Members
  • 122 posts

Posted 06 March 2013 - 11:46 AM

v is a table containing the returned values from a os.pullEventRaw()

crap , that may be the problem *fixes v to become votes

#6 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 06 March 2013 - 11:50 AM

View Postetopsirhc, on 06 March 2013 - 11:46 AM, said:

v is a table containing the returned values from a os.pullEventRaw()

crap , that may be the problem *fixes v to become votes
Nice, that would be the problem :P

#7 etopsirhc

  • Members
  • 122 posts

Posted 06 March 2013 - 11:53 AM

yeah lol *feels like a noob again* :P
way too used to simple letter variables , need to drop that habbit

#8 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 06 March 2013 - 11:59 AM

View Postetopsirhc, on 06 March 2013 - 11:53 AM, said:

yeah lol *feels like a noob again* :P
way too used to simple letter variables , need to drop that habbit
Everyone makes mistakes (Well normal people), even I do it sometimes. Hence the reason we have Ask A Pro ;)
Lol, when did you get in the habbit of using simple letter variables? (Off track) Oh well, just glad I could be of some help :)

EDIT: Impressed you didn't get mixed up with the io and fs libraries like most people... Time to go learn Objective-C! (Like that's gonna happen :P)

#9 etopsirhc

  • Members
  • 122 posts

Posted 06 March 2013 - 12:10 PM

i kinda got into it first for c++ style for statements
for (int i = 0; i <= 100; i++) { ect...
then lazyness took over causing the rest to follow

i've used both b4, usualy i go with fs unless i need to flush info to a file that wont ever close until the program exits

#10 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 06 March 2013 - 12:12 PM

View Postetopsirhc, on 06 March 2013 - 12:10 PM, said:

i kinda got into it first for c++ style for statements
for (int i = 0; i <= 100; i++) { ect...
then lazyness took over causing the rest to follow

i've used both b4, usualy i go with fs unless i need to flush info to a file that wont ever close until the program exits
Lol, nice. Definately break that nasty habbit :P I use the io library since it is used outside of CC and therefore a better habbit ;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users