#65351 - 2002-05-10 09:08 AM
Defining a new variable name from another variable?
|
Vig
Starting to like KiXtart
Registered: 2001-11-14
Posts: 166
Loc: Saudi Arabia
|
Say $var1 = "Test1" And I want a variable named "Test1" How would I do this?
A better example might be.
I do a readline to an open file and it returns the string "Test1". I want the script to create a variable named Test1 now. Is this possible?
I'm tired, maybe none of this makes sense.
|
|
Top
|
|
|
|
#65353 - 2002-05-10 05:54 PM
Re: Defining a new variable name from another variable?
|
Vig
Starting to like KiXtart
Registered: 2001-11-14
Posts: 166
Loc: Saudi Arabia
|
Thanks, I got this code to return the desired output of "12345678910"
code:
break on $counter = 1 do $var = "test" + "$counter" $ = execute("$$$var = $counter") $counter = $counter+1 until $counter = 11 $test1 $test2 $test3 $test4 $test5 $test6 $test7 $test8 $test9 $test10
Now what I'm trying to use this for is a kixform script. What it's supposed to do is create a box that has a check box for each entry in the test.dat file. Here is what I have.
test.kix
code:
;Script test.kix
$Form = CreateObject("Kixtart.Form") $Form.Caption = "Modify User Details - KiXtart" $Form.ScaleHeight = 300 $Form.ScaleWidth = 250 $Form.FontName = "Arial" $Form.FontSize = 9 $Form.Center
$Topdistance = 20 $nul = open(1,test.dat,2) $var = readline(1) do execute(" $$$var = $form.CheckBox("$var") ") execute(" $$$var+.Left= 10") execute(" $$$var+.TabStop= 0") execute(" $$$var+.Top= $Topdistance") execute(" $$$var+.OnClick= "chkAccountDisabled_Click()"") execute(" $$$var+.Enabled= 1") execute(" $$$var+.Value= 1") $Topdistance = $topdistance + 15 $var = readline(1) until $var = "" close(1)
$cmdExit = $form.CommandButton("Install!") $cmdExit.FontSize = 12 $cmdExit.FontBold = 1 $cmdExit.Top = 250 $cmdExit.Width = 100 $cmdExit.Left = 75 $cmdExit.OnClick = "Return_to_script()"
$Form.Show
While $quit <> 1 and $Form.Visible $=Execute($Form.DoEvents) Loop
Function chkAccountDisabled_Click() If $chkAccountDisabled.Value = 0 ? "Checkbox was not checked" Else ? "Checkbox was checked" EndIf EndFunction
Function Return_to_script() $quit = 1 EndFunction
In the test.dat
code:
qwerty asdfgfh asdfgfgh wrgdfhhv sdfhgfhj
Whenever I run the script I get this error.
Script error : invalid method/function call: missing comma ! execute(" $$$var = $form.checkbox("$var") ")
What am I missing here?
I realize that the onclick return will be sporked, but I'm not worried about that for now. I just wast to get the box to pop up.
|
|
Top
|
|
|
|
#65357 - 2002-05-10 06:41 PM
Re: Defining a new variable name from another variable?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Yeah, this is Jochens area of expertise to be sure, sorry J
I made a couple of small changes to your code (guess you caught them as well) and successfully ran it here - sweat !!!
-Shawn
|
|
Top
|
|
|
|
#65359 - 2002-05-10 08:01 PM
Re: Defining a new variable name from another variable?
|
Vig
Starting to like KiXtart
Registered: 2001-11-14
Posts: 166
Loc: Saudi Arabia
|
Actually I have absolutly no expierence in programing. Using kixform was my alternative to learning a programing language (if that made any sense).
Arrays, I haven't messed around with them so I know little about them.
I don't know if using dynamic variables painted me into a corner this time but it sure lead me to another problem. Each check box's onclick string is pointed at one function. Since the $var was lost during the do until statement I cannot get a return from $var.value. If there were a way to set another variable in the onclick string then this would work. Something like code:
$=execute(' $$$var.OnClick= $value = $counter+1 "chkAccountDisabled_Click()"')
This way I could make the function tell the difference between each checkbox, but still the function would have no idea what the value of the checkbox is (again the $var in $var.value is lost). I think I'm confusing myself at this point.
Any ideas?
|
|
Top
|
|
|
|
#65360 - 2002-05-10 09:00 PM
Re: Defining a new variable name from another variable?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Vig,
That's one corner I was thinking about, the other corner was if you wanted to enumerate all your checkboxes, but here's one solution:
1) Add $var as a function parm in your onclick:
code:
$=execute('$$$var.OnClick= "chkAccountDisabled_Click($$var)"')
2) Modify your function declaration like so:
code:
Function chkAccountDisabled_Click($var)
3) Change your message to this:
code:
? "$var checkbox was not checked"
Now when you click on a check box, the caption gets displayed on the console, hope this helps:
-Shawn
|
|
Top
|
|
|
|
#65361 - 2002-05-10 10:57 PM
Re: Defining a new variable name from another variable?
|
Vig
Starting to like KiXtart
Registered: 2001-11-14
Posts: 166
Loc: Saudi Arabia
|
I'm missing something. I did the changes you suggested and I get an error when I click on a check box.
Script error : unknown command If $var.Value = 0
If it's not to much trouble could please explain the use of the () in a function call?
Here is how my script looks. code:
$Form = CreateObject("Kixtart.Form") $Form.Caption = "Insatll Stuff" $Form.ScaleHeight = 300 $Form.ScaleWidth = 250 $Form.FontName = "Arial" $Form.FontSize = 9 $Form.Center
$Topdistance = 20 $nul = open(1,test.dat,2) $var = readline(1)
do $=execute(' $$$var = $$form.CheckBox("$var") ') $=execute(" $$$var.Left= 10") $=execute(" $$$var.TabStop= 0") $=execute(" $$$var.Top= $Topdistance") $=execute(' $$$var.OnClick= "chkAccountDisabled_Click($$var)"') $=execute(" $$$var.Enabled= 1") $=execute(" $$$var.Value= 1") $Topdistance = $topdistance + 20 $var = readline(1) until $var = "" $=close(1)
$cmdExit = $form.CommandButton("Install!") $cmdExit.FontSize = 12 $cmdExit.FontBold = 1 $cmdExit.Top = 250 $cmdExit.Width = 100 $cmdExit.Left = 75 $cmdExit.OnClick = "Return_to_script()"
$Form.Show
While $quit <> 1 and $Form.Visible $=Execute($Form.DoEvents) Loop
Function chkAccountDisabled_Click($var)
If $var.Value = 0 ? "$var Checkbox was not checked" Else ? "$var Checkbox was checked" EndIf EndFunction
Thanks
|
|
Top
|
|
|
|
#65362 - 2002-05-10 11:53 PM
Re: Defining a new variable name from another variable?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Vig,
Try this version. In order for this to work, you have to silence all your function calls because the $= assigns are messing-up the evaluations.
Normally this isn't a problem, but i think because the string is being evaluated twice (once in the execute, and again in the assign) - things are getting hairy - sure you don't want to go for the control array approach ?
Jochen wrote a script that uses this strategy quite a bit, I'm hoping he can step in and explain better what is going on. In the meantime, I've silenced your functions, changed the onclick assign, and modified your click function, try this one:
code:
break on
$Form = CreateObject("Kixtart.Form") $Form.Caption = "Insatll Stuff" $Form.ScaleHeight = 300 $Form.ScaleWidth = 250 $Form.FontName = "Arial" $Form.FontSize = 9 $Form.Center
$Topdistance = 20 $nul = open(1,test.dat,2) $var = readline(1)
do $n=execute(' $$$var = $$form.CheckBox("$var") ') $n=execute(" $$$var.Left= 10") $n=execute(" $$$var.TabStop= 0") $n=execute(" $$$var.Top= $Topdistance") $n=execute(' $$$var.OnClick= "chkAccountDisabled_Click('+'$$$'+'$var)"') $n=execute(" $$$var.Enabled= 1") $n=execute(" $$$var.Value= 1") $Topdistance = $topdistance + 20 $var = readline(1) until $var = "" $n=close(1)
$cmdExit = $form.CommandButton("Install!") $cmdExit.FontSize = 12 $cmdExit.FontBold = 1 $cmdExit.Top = 250 $cmdExit.Width = 100 $cmdExit.Left = 75 $cmdExit.OnClick = "Return_to_script()"
$Form.Show
While $quit <> 1 and $Form.Visible $= Execute($Form.DoEvents) loop
Function chkAccountDisabled_Click($var)
If $var.Value = 0 ? $var.caption + " was not checked" Else ? $var.caption + " was checked" EndIf EndFunction
By the way, what version of Kixtart are your using ? What version of Kixforms ? To determine the Kixforms version, start a form then click the Window Title title close box (top left) - then click "About Kixforms..." ...
-Shawn [ 10 May 2002, 23:55: Message edited by: Shawn ]
|
|
Top
|
|
|
|
#65363 - 2002-05-11 01:02 AM
Re: Defining a new variable name from another variable?
|
Vig
Starting to like KiXtart
Registered: 2001-11-14
Posts: 166
Loc: Saudi Arabia
|
Kixform release 11 Kixtart ver 4.00
I tried your script and it worked great. I'm going to take a look at the changes here in a minute.
Over the past couple hours I was working on a solution and this is what I came up with. code:
;Script test.kix
$Form = CreateObject("Kixtart.Form") $Form.Caption = "Insatll Stuff" $Form.ScaleHeight = 300 $Form.ScaleWidth = 250 $Form.FontName = "Arial" $Form.FontSize = 9 $Form.Center
$Topdistance = 20 $nul = open(1,test.dat,2) $var = readline(1) $counter = 1
do $=execute(' $$$var = $$form.CheckBox("$var") ') $=execute(" $$$var.Left= 10") $=execute(" $$$var.TabStop= 0") $=execute(" $$$var.Top= $Topdistance") $=execute(' $$$var.OnClick= "chkAccountDisabled_Click($counter)"') $=execute(" $$$var.Enabled= 1") $=execute(" $$$var.Value= 1") $Topdistance = $topdistance + 20 $var = readline(1) $counter = $counter + 1 until $var = "" $=close(1)
$cmdExit = $form.CommandButton("Install!") $cmdExit.FontSize = 12 $cmdExit.FontBold = 1 $cmdExit.Top = 250 $cmdExit.Width = 100 $cmdExit.Left = 75 $cmdExit.OnClick = "Return_to_script()"
$Form.Show
While $quit <> 1 and $Form.Visible $=Execute($Form.DoEvents) Loop
Function chkAccountDisabled_Click($counter)
If $counter <> 0 $=open(1,test.dat,2) $counter2 = 0 do $var=readline(1) $counter2 = $counter2 + 1 until $counter = $counter2 $=close(1) Else $counter = 0 ? "Error in counter" EndIf If $$$var.value = 0 ;<---------------------------------------------------------------- HERE ? "$var Checkbox was not checked" Else ? "$var Checkbox was checked" EndIf EndFunction
Function Return_to_script() $quit = 1 EndFunction
I've got everything working they way it should ($var even = the checkbox selected), but theres one line I just cant get to work right. It's one of the simple ones to but I've played around with it and I can't get to to work. It's the line marked HERE in the code.
It's got to be the syntax because the value of $var is the box selected.
BTW what is the control array approach?
Thanks
|
|
Top
|
|
|
|
#65364 - 2002-05-11 01:18 AM
Re: Defining a new variable name from another variable?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Oh my ! I think this statement breaks the world record for the most consecutive number of required double dollar signs. Insert this UDF into your script:
code:
Function chkAccountDisabled_Click($counter)
If $counter <> 0 $=open(1,test.dat,2) $counter2 = 0 do $var=readline(1) $counter2 = $counter2 + 1 until $counter = $counter2 $=close(1) Else $counter = 0 ? "Error in counter" EndIf
$n = Execute(' If $$$var.value = 0 ? "$$$$$var Checkbox was not checked" ; <--- LQQKIE Else ? "$$$$$var Checkbox was checked" EndIf ')
EndFunction
Here's a link to a control array script:
Kixforms: KixMenu - Dynamically build button menus from INI configuration files
You might want to think about using INI files for this project, that way, you could store extra info about each checkbox (install) in there as well.
-Shawn
p.s. Why so many $ signs ? Because the string is evaluated twice. Once by execute, then once as it gets piped to the console - thats my story and I'm sticking to it ! [ 11 May 2002, 01:25: Message edited by: Shawn ]
|
|
Top
|
|
|
|
#65365 - 2002-05-11 02:37 AM
Re: Defining a new variable name from another variable?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Vig,
Just so that you can compare, here's a very quick and dirty conversion into the control array approach. Not sure if you agree, but (I) think its a little easier on the eyes (and the noggin) ...
The data file is an INI file like this:
[TEST.INI]
code:
[item1]
[item2]
[item3]
[item4]
[item5]
and the script:
code:
break on $profile = ".\test.ini" $form = createobject("kixtart.form") $form.caption = "Install Stuff" $form.scaleheight = 300 $form.scalewidth = 250 $form.fontname = "Arial" $form.fontsize = 9 $form.center $topdistance = 20 global $checkboxes[20] $i = 0 for each $item in split(readprofilestring($profile,"",""),CHR(10)) if $item $checkboxes[$i] = $form.checkbox($item,10,$topdistance) $checkboxes[$i].tabstop = 0 $checkboxes[$i].value = 1 $checkboxes[$i].onclick = "checkbox_click($$checkboxes[$i])" $topdistance = $topdistance + 20 $i=$i+1 endif next if $i redim preserve $checkboxes[$i-1] endif $list = $form.commandbutton("List all checkboxes",25,200,200,25) $list.fontsize = 12 $list.fontbold = 1 $list.onclick = "list_checkboxes()" $exit = $form.commandbutton("Install!") $exit.fontsize = 12 $exit.fontbold = 1 $exit.top = 250 $exit.width = 100 $exit.left = 75 $exit.onclick = "Return_to_script()" $form.show while not $quit and $form.visible $=execute($form.doevents) loop exit 1 function checkbox_click($checkbox) if $checkbox.value ? $checkbox.caption " is checked" else ? $checkbox.caption " is not checked" endif endfunction function list_checkboxes for each $checkbox in $checkboxes ? $checkbox.caption " = " $checkbox.value next endfunction function return_to_script() $quit = 1 endfunction
Hope this helps!
-Shawn [ 11 May 2002, 03:13: Message edited by: Shawn ]
|
|
Top
|
|
|
|
#65366 - 2002-05-12 12:26 AM
Re: Defining a new variable name from another variable?
|
Vig
Starting to like KiXtart
Registered: 2001-11-14
Posts: 166
Loc: Saudi Arabia
|
Thanks
Now to make sense of it.
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1821 anonymous users online.
|
|
|