Page 1 of 1 1
Topic Options
#184763 - 2008-01-22 10:34 PM Access one variable with the value of another
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Say I have a variable, $obscure for example, and the value of $obscure is “c:\windows\some\obscure\path”.

Now, let’s say that I read a file that contains locations that I need to look to and do stuff. If one of the locations that I pull from the file is the word “obscure” and I set that to a variable named $filelocation, I now have a variable ($filelocation) with a value that is the name of another variable ($obscure). Is there any way that I can access the value of $obscure by using the variable $filelocation?

I hope this makes sense.

Top
#184764 - 2008-01-22 10:45 PM Re: Access one variable with the value of another [Re: JohnQ]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
So you are trying to say....

 Code:
$Obscure = "C:\Windows\Some\Obscure\Patch"
$FileLocation = "Obscure"

If IsDeclared(Chr(36)+$FileLocation)
  ; do Stuff
EndIf
_________________________
Today is the tomorrow you worried about yesterday.

Top
#184765 - 2008-01-22 10:49 PM Re: Access one variable with the value of another [Re: Gargoyle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
You might want to read about the EXECUTE command. Seems like it might be useful in this situation.

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

Top
#184768 - 2008-01-22 11:04 PM Re: Access one variable with the value of another [Re: JohnQ]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
I'm having trouble understanding what you want to do.

Might you be willing to explain what you are trying to accomplish. Maybe there is another way.

Top
#184769 - 2008-01-22 11:25 PM Re: Access one variable with the value of another [Re: Gargoyle]
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
That's exactly what I'm trying to say, but unfortunately ISDECLARED doesn't see Chr(36)+$FileLocation the same as the variable $Obscure - even though they should be.

Glen mentioned using EXECUTE, but in this case I'm sure what I would execute.

Top
#184771 - 2008-01-22 11:38 PM Re: Access one variable with the value of another [Re: JohnQ]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Like Allen I'm having trouble understanding what you want to do

If I understand correctly you want to see if the contents of the $obscure variable is the same as the contents of the $filelocation variable?
If so this would be the way to go.

 Code:
If $FileLocation = $Obscure
	;Do stuff
Else
	;Do other stuff
EndIf
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#184773 - 2008-01-23 12:21 AM Re: Access one variable with the value of another [Re: Mart]
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
The values of $FileLocation and $Obscure will never be the same. Let me try to give a better example.

To keep it simple, let's say I need to copy some files to one of 10 locations. I have 10 variables set -
$location1 = “c:\windows\some\obscure\path”
$location2 = “c:\program files\some\other\path”
etc...

The only way that I know where to copy the files to is to read an external file which will contain the location. This will NOT be the full path - only one word. For the sake of this example, let's say the file contains the word "location2". Now I know where I need to copy the files to and have a new variable - $x=location2. Lastly, I need to string together the command to copy the files and somehow match up the value of $x with the variable with the matching name - $location2.

Top
#184774 - 2008-01-23 12:44 AM Re: Access one variable with the value of another [Re: JohnQ]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
I think I understand. Show us a couple of the values from the external file you will be pulling from.
Top
#184776 - 2008-01-23 01:02 AM Re: Access one variable with the value of another [Re: Allen]
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
The values from the external file could be anything, but the value in the external file will always match the name of a variable in the script -- just like in my previous example where the value in the external file was "location2" and there was a variable in the script named $location2.
Top
#184777 - 2008-01-23 01:10 AM Re: Access one variable with the value of another [Re: JohnQ]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
I'm sorry I don't have time to explain this, as I needed to be somewhere 10 minutes ago... but see if this gets you going. It's using a two dimensional array...

 Code:
dim $locations[1,10]


$locations[0,0]="location1"
$Locations[1,0]="c:\windows\some\obscure\path"

$locations[0,1]="location2"
$Locations[1,1]="c:\program files\some\obscure\path"

$x="location2"

for $i=1 to ubound($locations,2)
  if $x=$locations[0,$i]
    ? "X goes here:  " + $locations[1,$i]
  endif
next

Top
#184779 - 2008-01-23 01:25 AM Re: Access one variable with the value of another [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Lets step away from your specific problem for a moment and get our minds around the concept.. (oooohhhhmmmmmmm) Ready?

You have, for example, two variables. For the excercise, lets call them A and B. Some input tells you which variable to use. Therein lies the problem, variables can't DIRECTLY reference variables.

Try this simple example, and I bet you will be able to solve your problem.
 Code:
Break On

$ = SetOption('NoVarsInStrings', 'On')

$A = 'Yes'
$B = 'No'

'Which variable would you like to see ("A" or "B")? '
Get $Answer
? ?

$ = Execute('$ActiveValue = $' + $Answer)

'The value of $' $Answer ' is "' $ActiveValue '"' ?

So, now expand this process into your problem. You define, say, 10 values, $A through $J. Your file says to use "E". You use the Execute code to assign the value of E to a working variable - $ActiveValue in this example.

I'll let you play on your own for a bit - it's not too hard to convert this to what you want. Stop back if you need more clues.

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

Top
#184784 - 2008-01-23 05:00 AM Re: Access one variable with the value of another [Re: Glenn Barnas]
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
This is perfect Glen - exactly what I needed.

I should have known that your first post regarding EXECUTE was a clue and not just a suggestion.

Top
#184789 - 2008-01-23 01:04 PM Re: Access one variable with the value of another [Re: JohnQ]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Glad it worked. I didn't have time to elaborate in my first post. I also prefer to leave some detective work to the poster.

How about posting your final solution when you get it done?

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

Top
#186895 - 2008-04-14 05:23 PM Re: Access one variable with the value of another [Re: Glenn Barnas]
nocke62 Offline
Fresh Scripter

Registered: 2005-03-30
Posts: 12
Loc: Ratingen, Germany
Hello Glenn,

is there a way, to do this with arrays too?
When I try this, I get the error: Error in Expression: this type of array not supported in expressions!

Thanks,

Wolfgang

Top
#186896 - 2008-04-14 06:28 PM Re: Access one variable with the value of another [Re: nocke62]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Maybe something like this will get you started:
 Code:
Break On

$ = SetOption('NoVarsInStrings', 'On')

$A = 'Yes', 'No', 'Maybe'
$B = 'No', 'Maybe', 'Yes'

'Which variable would you like to see ("A" or "B")? '
Get $Answer
$Answer ?
'Which Index do you want to use (0-2)? '
Get $Index
$Index
? ?

$ = Execute('$ActiveValue = $' + $Answer +'[$Index]')

'The value of $' $Answer ' is "' $ActiveValue '"' ?


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

Top
#186920 - 2008-04-15 12:03 PM Re: Access one variable with the value of another [Re: Glenn Barnas]
nocke62 Offline
Fresh Scripter

Registered: 2005-03-30
Posts: 12
Loc: Ratingen, Germany
Hello Glenn,

thanks for your quick response.

After my post, later on in the evening, I found this solution shown in the example below.

 Code:
$ = SetOption('NoVarsInStrings', 'On')

$array_1_count = 2
dim $array_1[$array_1_count -1 ,2]

$array_1[0,0] = "language"
$array_1[0,1] = "de"
$array_1[0,2] = "REG_SZ"

$array_1[1,0] = "client"
$array_1[1,1] = "thin"
$array_1[1,2] = "REG_SZ"


$name = "array_1"

$ = Execute('$ActiveValue = $' + $name)

for $i = 0 to $array_1_count -1 step 1
	$a_key = $activeValue[$i,0]
	$a_val = $activeValue[$i,1]
	$a_typ = $activeValue[$i,2]

	?
	? "a_key: " + $a_key
	? "a_val: " + $a_val
	? "a_typ: " + $a_typ
next


Thanks

Wolfgang

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 837 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.073 seconds in which 0.027 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