#185950 - 2008-03-05 12:39 AM
Using isdeclared to check a variable inside a variable
|
lukeod
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.
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
|
|
|
|
#185952 - 2008-03-05 01:08 AM
Re: Using isdeclared to check a variable inside a variable
[Re: Gargoyle]
|
lukeod
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-
$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
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
|
|
|
|
#185954 - 2008-03-05 01:48 AM
Re: Using isdeclared to check a variable inside a variable
[Re: Glenn Barnas]
|
lukeod
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
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:
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:
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:
$a = EXECUTE( isdeclared($tempvar))
Same result.
Also tried:
$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
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)
$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
KiX Supporter
   
Registered: 2003-04-19
Posts: 4567
Loc: USA
|
Oopps forgot something...
$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
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':
$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
|
|
|
|
#185961 - 2008-03-05 04:50 AM
Re: Using isdeclared to check a variable inside a variable
[Re: Glenn Barnas]
|
lukeod
Getting the hang of it
Registered: 2008-01-11
Posts: 70
Loc: Australia
|
Cheers Glenn.
I'm very, very close!
My 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:
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
|
|
|
|
#185963 - 2008-03-05 05:27 AM
Re: Using isdeclared to check a variable inside a variable
[Re: Allen]
|
lukeod
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:
$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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 476 anonymous users online.
|
|
|