Page 1 of 1 1
Topic Options
#185950 - 2008-03-05 12:39 AM Using isdeclared to check a variable inside a variable
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Hello all,

I'm hoping there is a way to check to see if the string inside a variable is declared, i suppose it's best described with an example. Note, the below code isnt really for practical purposes, rather i just put it together in 2 mins as a 'simple' example, may have syntax errors etc buy i'm not worried about them at the moment, rather the concept.

 Code:

DIM $variable1, $variable2, $variable3, $variable4, $tempvar, $varcheck, $counter, $varprefix
$variable1 = "abc"
$variable2 = "def"
$variable3 = "ghi"
$variable4 = "jkl"
$varprefix = "$$variable"
$counter = 0
$tempvar = 0
$varcheck = 0
 
Do
	$counter = $counter + 1
	$tempvar = $varprefix + $counter
	"checking to see if the variable (" + $tempvar + ") is declared" ?
	IF isdeclared($tempvar)
		"do Stuff"?
		$varcheck = 1

	ELSE
		$varcheck = 0
	ENDIF	
	

Until $varcheck = 0



Basically what happenes (as it should), is that it is checking to see if the variable '$tempvar' is declared, rather than the name of the variable that it is holding. Is there a way of saying 'check whats inside $tempvar' or somthing that will give that effect?

Thanks in advance

Luke


Edited by lukeod (2008-03-05 12:40 AM)

Top
#185951 - 2008-03-05 12:51 AM Re: Using isdeclared to check a variable inside a variable [Re: lukeod]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
 Code:

InStr($TempVar,"WhatIAmLookingFor")
_________________________
Today is the tomorrow you worried about yesterday.

Top
#185952 - 2008-03-05 01:08 AM Re: Using isdeclared to check a variable inside a variable [Re: Gargoyle]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Oh sorry Gargoyle, i didnt explain myself very well in the last line of my post, i do want to see if $variable[x] is declared, rather than see whats inside the string. -ie-

 Code:
$variable1
$tempvar = "$$variable1"

isdeclared($tempvar)


The result i'm after is it returning "yes $variable1 is declared" rather than "yes $tempvar is declared"

I want to know if you can use a statement similar to above without saying directly
 Code:
isdeclared($variable1)


The reason behind it is using a loop to go though and check if variables are declared.

Hmm, i might have a look and see if i can use an array to get the results i want.

Cheers for the reply


Edited by lukeod (2008-03-05 01:10 AM)

Top
#185953 - 2008-03-05 01:34 AM Re: Using isdeclared to check a variable inside a variable [Re: lukeod]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
You might want to look at "Execute"... You can execute a line of Kix code, and it will expand the vars. Pretty cool capability, but takes a bit of getting used to.

Give it a try, and post back if you need more help.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#185954 - 2008-03-05 01:48 AM Re: Using isdeclared to check a variable inside a variable [Re: Glenn Barnas]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Okay will do Glenn, havnt used 'execute' before, will have to do a bit of reading.

Thanks for the info.

Top
#185956 - 2008-03-05 03:49 AM Re: Using isdeclared to check a variable inside a variable [Re: lukeod]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
I have tried 'Execute' with a few variations, but no cigar \:\( .

First attempt:

 Code:
 
REDIRECTOUTPUT("C:\temp\loopcheck.txt",1)
BREAK ON

DIM $variable1, $variable2, $variable3, $variable4, $tempvar, $varcheck, $counter, $varprefix
$variable1 = "abc"
$variable2 = "def"
$variable3 = "ghi"
$variable4 = "jkl"
$varprefix = "$$variable"
$counter = 0
$tempvar = 0
$varcheck = 1
$a = ""

Do
	$counter = $counter + 1
	$tempvar = $varprefix + $counter
	"checking to see if the variable (" + $tempvar ") is declared" ?
	$a = EXECUTE( isdeclared('$$varprefix + $$counter'))
	" a is... (" + $a + ")" ?
	sleep 0.5
Until $varcheck = 0    ;at the moment it's looping till the cows come home



It spits out a text file (redirectoutput) looking like:

 Code:
checking to see if the variable ($variable1) is declared
0
 a is... (0)
checking to see if the variable ($variable2) is declared
0
 a is... (0)
checking to see if the variable ($variable3) is declared
0
 a is... (0)
checking to see if the variable ($variable4) is declared
0
 a is... (0)
checking to see if the variable ($variable5) is declared
0
 a is... (0)
checking to see if the variable ($variable6) is declared
0
 a is... (0)
checking to see if the variable ($variable7) is declared
0
 a is... (0)
checking to see if the variable ($variable8) is declared
0


I also tried replacing the 'Execute' line with:

 Code:
$a = EXECUTE( isdeclared($tempvar))


Same result.

Also tried:

 Code:
	
$a = EXECUTE( isdeclared('$$tempvar'))



Am i way off course here?

Cheers

Luke


Edited by lukeod (2008-03-05 03:50 AM)

Top
#185957 - 2008-03-05 03:59 AM Re: Using isdeclared to check a variable inside a variable [Re: lukeod]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
Whenever I am messing with Execute, the very first thing I do, is turn on NoMacrosinStrings, and NoVarsinStrings... I think it makes it easier to build the execute line:

(untested)
 Code:
$RC=setoption("NoVarsinstrings","on")
$RC=setoption("NoMacrosinstrings","on")

$a = EXECUTE('isdeclared($varprefix' + $counter + ')')

Top
#185958 - 2008-03-05 04:06 AM Re: Using isdeclared to check a variable inside a variable [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
Oopps forgot something...

 Code:
$a=EXECUTE('$isdeclared=isdeclared($varprefix' + $counter + ')')
? $isdeclared

Top
#185959 - 2008-03-05 04:13 AM Re: Using isdeclared to check a variable inside a variable [Re: Allen]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Thanks for the tip.

Tried that execute line, but it returned exactly the same thing. I also tried this very obviously incorrect line of code just to see what it'd do - trying to get somthing to return a value of $a not equal to '0':

 Code:
	$a = EXECUTE('isdeclared($varprefixaa)') 


It also returned '0'.

Very odd, i cant find much info on exactly what 'Execute' does in either the command reference or manual \:\(

Top
#185960 - 2008-03-05 04:17 AM Re: Using isdeclared to check a variable inside a variable [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
right idea, but missing something in the execution...

Allen has some good points, but I'll add my $0.02 as well..

Create a string to execute, and pass it to the execute function. Before you execute it, display it to the console and see if the command makes sense to you. If it doesn't, it won't make sense to Execute either! Something along the lines of:
 Code:
$Cmd = this and that...
$Cmd ?
$A = Execute($Cmd)

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#185961 - 2008-03-05 04:50 AM Re: Using isdeclared to check a variable inside a variable [Re: Glenn Barnas]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Cheers Glenn.

I'm very, very close!

My Code:

 Code:
$RC=setoption("NoVarsinstrings","on")
$RC=setoption("NoMacrosinstrings","on")

REDIRECTOUTPUT("C:\temp\loopcheck.txt",1)


BREAK ON

DIM $variable1, $variable2, $variable3, $variable4, $tempvar, $varcheck, $counter, $varprefix, $array[1], $cmd
$variable1 = "abc"
$variable2 = "def"
$variable3 = "ghi"
$variable4 = "jkl"
$varprefix = "$variable"
$counter = 0
$tempvar = 0
$varcheck = 1
$a = ""
$cmd = ""

Do
	$counter = $counter + 1
	$tempvar = $varprefix + $counter
	"checking to see if the variable (" + $tempvar + ") is declared" ?
	$cmd = "isdeclared(" + $varprefix + $counter + ")"
	"CMD is: " + $cmd  ?
	"Error Before EXECUTE is " + @error ?
	$a = EXECUTE($cmd)
	"Error Code after Execute is: (" + @ERROR + ")" ?
	"Execute leave code is:(" + $a + ")" ?
	"**********************" ?

sleep 0.2

Until $varcheck = 0



Returns a text file looking like:

 Code:
0checking to see if the variable ($variable1) is declared
CMD is: isdeclared($variable1)
Error Before EXECUTE is 0
1Error Code after Execute is: (0)
Execute leave code is:(0)
**********************
checking to see if the variable ($variable2) is declared
CMD is: isdeclared($variable2)
Error Before EXECUTE is 0
1Error Code after Execute is: (0)
Execute leave code is:(0)
**********************
checking to see if the variable ($variable3) is declared
CMD is: isdeclared($variable3)
Error Before EXECUTE is 0
1Error Code after Execute is: (0)
Execute leave code is:(0)
**********************
checking to see if the variable ($variable4) is declared
CMD is: isdeclared($variable4)
Error Before EXECUTE is 0
1Error Code after Execute is: (0)
Execute leave code is:(0)
**********************
checking to see if the variable ($variable5) is declared
CMD is: isdeclared($variable5)
Error Before EXECUTE is 0
0Error Code after Execute is: (0)
Execute leave code is:(0)
**********************
checking to see if the variable ($variable6) is declared
CMD is: isdeclared($variable6)
Error Before EXECUTE is 0
0Error Code after Execute is: (0)
Execute leave code is:(0)
**********************


Notice that before the line (text file) "Error after Execute is:" it returns a 1 if the variable exists, and a 0 if it dosnt. This is the variable i'm chasing, except i dont know how to use it!! @error and $a dont appear to be this rogue character. Any ideas on what this variable is and how i could then use it in an IF THEN ELSE statement?

Thanks for the help so far \:\)

Luke

Top
#185962 - 2008-03-05 04:56 AM Re: Using isdeclared to check a variable inside a variable [Re: lukeod]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
Did you see my follow up post above? You need to add a var to the execute statement.
Top
#185963 - 2008-03-05 05:27 AM Re: Using isdeclared to check a variable inside a variable [Re: Allen]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
No i didn't sorry Allen. You were correct, it now works.

Code for anyone interested is:

 Code:
$RC=setoption("NoVarsinstrings","on")
$RC=setoption("NoMacrosinstrings","on")
REDIRECTOUTPUT("C:\temp\loopcheck.txt",1)


BREAK ON

DIM $variable1, $variable2, $variable3, $variable4, $tempvar, $varcheck, $counter, $varprefix, $cmd
$variable1 = "abc"
$variable2 = "def"
$variable3 = "ghi"
$variable4 = "jkl"
$varprefix = "$variable"
$counter = 0
$tempvar = 0
$varcheck = 1
$cmd = ""
$isdeclared = ""

Do
	$counter = $counter + 1
	$tempvar = $varprefix + $counter
	"checking to see if the variable (" + $tempvar + ") is declared" ?
	$cmd = "$isdeclared = isdeclared(" + $varprefix + $counter + ")"
	EXECUTE($cmd)
	IF $isdeclared = 1
		$varcheck = 1
		"Variable is declared" ?
	ELSE
		$varcheck = 0
		"Variable is not declared, exiting." ?
	ENDIF
	"**********************" ?


Until $varcheck = 0


Thanks for the help guys \:\)

One last question, what is the command to supress the error codes from automatically being displayed on the script? -ie- if i use the "use" command and it is successful, it outputs '0' to the console / text file if being piped. Anyway of preventing this?

Luke


Edited by lukeod (2008-03-05 05:27 AM)

Top
#185964 - 2008-03-05 06:55 AM Re: Using isdeclared to check a variable inside a variable [Re: lukeod]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Yes you can supress the screen output, you cand find a well detailed explanation of why they happen in the FAQ's, but suffice it to say for now, that anytime you peform a function you have to catch it with a variable

$rc = USE ...
_________________________
Today is the tomorrow you worried about yesterday.

Top
#185968 - 2008-03-05 09:26 AM Re: Using isdeclared to check a variable inside a variable [Re: Gargoyle]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: Gargoyle
Yes you can supress the screen output, you cand find a well detailed explanation of why they happen in the FAQ's, but suffice it to say for now, that anytime you peform a function you have to catch it with a variable

$rc = USE ...


Both right... and a bit wrong, as you were led astray by the OP.

You are absolutely correct to say that you "suppress" the return values of a function by assigning to a variable.

However USE is a command, not a function and as such it does not return a value (it does set @ERROR). Attempting to assign it to a variable is syntactically incorrect.

If the OP is getting codes on the screen it is from something other than USE.

Top
#185978 - 2008-03-05 01:06 PM Re: Using isdeclared to check a variable inside a variable [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Yes indeed, Richard, it is a function who's result is not being captured - and in the very first few lines, too!

Luke - if you properly closed your output stream in the same manner, you'd have a stray "0" on the console, too.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1376 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.083 seconds in which 0.034 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org