Page 1 of 1 1
Topic Options
#163131 - 2006-06-09 08:22 PM Can you make a variable's value it's own variable?? $$foo?
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
if $foo has a value in a loop, is there a way to make $foo it's own variable?? In PHP, $$foo would work but it didn't work in KiX. In that case, if $foo had a value of 'MAC', then $MAC would be a new var because of $$foo. I don't know if I'm explaining it very well. Any thoughts?
Top
#163132 - 2006-06-09 08:33 PM Re: Can you make a variable's value it's own variable?? $$foo?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Can you give a real life example of what you are trying to accomplish?

Just as a rough guess VBS Dictionary objects come to mind as a possible solution. I have made several post related to HASHes and Dictionary objects for which you can search.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#163133 - 2006-06-09 08:37 PM Re: Can you make a variable's value it's own variable?? $$foo?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You can built the logic using the execute function.

Code:
break on

$a = "b"
$rc = execute ('$$' + $a + ' = "c"')
? "$$b = " + $b

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#163134 - 2006-06-09 08:40 PM Re: Can you make a variable's value it's own variable?? $$foo?
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
Ok, I have an INI file with these values: Code:
[General]
HostName=pc1
Timestamp=1149869020
MAC=000BDB82230F
LLU=cameron.wilson
OS=Windows XP Professional
Ver=Service Pack 2
ScriptRunTime=16



I'm using this loop to get that information: Code:
$General		= QS(Split(ReadProfileString($DatedFile,'General',''),chr(10)))


For Each $x in $General
$i = ReadProfileString($DatedFile,'General',$x)
? $$x
Next



Now my vars in the script match the INI element name. So if $x = 'MAC' in my script, then if I can turn $$x into $MAC, then I cancompare the actual MAC against the one stored in the INI file.... is that any better?


Edited by thepip3r (2006-06-09 08:40 PM)

Top
#163135 - 2006-06-09 08:54 PM Re: Can you make a variable's value it's own variable?? $$foo?
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
well after thinking about it a big more, i'm using it in the wrong context for how i use it in PHP. in PHP, when i have an associative array, I use the attempted-to-describe-above method for working with variables instead of array elements:

Code:
$test['1'] = right;
$test['2'] = wrong;
$test['3'] = either;

foreach ($test as $key => $val) {
$$key = $val
}



so then, $1 = right, $2 = wrong, etc. again, sorry for asking a question incorrectly. =/


Edited by thepip3r (2006-06-09 08:57 PM)

Top
#163136 - 2006-06-09 09:12 PM Re: Can you make a variable's value it's own variable?? $$foo?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Code:
break on
$INIfile = "c:\data\scripts\test.ini"
$General = Split(ReadProfileString($INIfile,'General',''),chr(10))

For Each $x in $General
if $x
$val = ReadProfileString($INIfile,'General',$x)
$val = "" + $val
$CMDstring = '$$' + $x + ' = "' + $val + '"'
$rc = execute ($CMDstring)
endif
Next

?
? $HostName
? $Timestamp
? $MAC
? $LLU
? $OS
? $Ver
? $ScriptRunTime



I just saw your new post. I will ook at it when I get off the phone.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#163137 - 2006-06-18 03:36 PM Re: Can you make a variable's value it's own variable?? $$foo?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I think it's generally a bad idea to dynamically generate variables names that are then used elsewhere in a script as in severely complicates maintenance. I'd rather use pre-defined variables or arrays.
_________________________
There are two types of vessels, submarines and targets.

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 1114 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.059 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

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