HyperMath

exist

exist

Previous topic Next topic No expanding text in this topic  

exist

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

Checks if a variable or a function exists.

Syntax

b = exist(str)

b = exist(str, 'local')

b = exist(str, 'base')

b = exist(str, 'global')

Argument

Name

Description

 

str

A string: a variable or function name.

 

'local', 'base' or 'global'

A string to specify the scope where you want to check the existing variable or function.

Outputs

Name

Description

 

b

0 (does not exist) or 1 (exists).

Example

Syntax

 

// Remove all previous variables defined

ClearAll();

foo = 12

// foo exists

if exist('foo') then

 print('variable foo exists.')

else

 print('variable foo does not exists')

end

global fuu;

fuu = 12;

global fee;

fee = 69;

function fii()

 global fuu;

 // foo variable does not exist in the scope of fii function

 if exist('foo') then

  print('variable \'foo\' exists.')

 else

  print('variable \'foo\' does not exists in this scope.')

 end

 if exist('foo', 'base') then

  print('variable \'foo\' exists in \'base\' scope.')

 else

  print('variable \'foo\' does not exists in \'base\' scope.')

 end

 

 // fuu variable is a global and exist in the scope of fii function

 if exist('fuu') then

  print('variable fuu exists.')

 else

  print('variable fuu does not exists')

 end

 faa = 3

 if exist('faa', 'local') then

  print('variable \'faa\' exists in this \'local\' scope.')

 else

  print('variable \'faa\' does not exists in \'local\' scope.')

 end 

 

 if exist('fee', 'global') then

  print('variable \'fee\' exists in this \'global\' scope.')

 else

  print('variable \'fee\' does not exists in \'global\' scope.')

 end 

end

fii()

print(exist('fii'));

 

Result

 

variable foo exists.

variable 'foo' does not exists in this scope.

variable 'foo' exists in 'base' scope.

variable fuu exists.

variable 'faa' exists in this 'local' scope.

variable 'fee' exists in this 'global' scope.

Comments

b = exist(variablename) returns 1 if the variable or function exists and 0 in other cases.

See Also:

type

what

who