Skip to content
⌘ NSIS Forum Archive

Variable Problem

8 posts

Jnuw#

Variable Problem

Hello all.

I'm having a little problem with how I'm trying to use a variable. Here is an example:

${MyArray->Shift} "VarA"
${MyArray->Shift} "VarB"
${MyArray->Shift} "VarC"

StrCpy $VarA yes
StrCpy $VarB no
StrCpy $VarC Yes

StrCpy $0 0
Loop:
IntCmp $0 3 Done 0 Done
${MyArray->Read} $1 $0

MessageBox MB_OK $$$1

IntOp $0 $0 + 1
Goto Loop

Done:
Basically, during the course of my script, a bunch of variables get set to yes or no (VarA, VarB, VarC). I have the list of these variables stored in an Array (MyArray). Several times during my script I want to check each of these variables (over 15 of them in reality), and instead of having 15 separate checks, I want to loop through them.

The problem is when I actually try to use the variable, in a MessageBox in the example above, I only get the variable name, and not its value. I'm using '$$$' above to add 1 $ to the result of the Array Read. So the first time through the loop, $1 gets set the VarA, and then is displayed as $VarA in the MessageBox. However I really want to see 'yes' in the message box.

I realize I'm really using one variable which points to a value that happens to be equal to another variable, and expecting to see the second variable's value. If I could somehow use nesting or concatenation to use the second variable initially, and get the desired value, that would be great. Let me know if I'm just crazy, and there is a much better way to do this.

Thanks for looking.

- Jnuw
Jnuw#
Hey guys. I figured out a work around, using another array. However, I ran into a similar problem a while back with attempting to nest variables, http://forums.winamp.com/showthread....ghlight=nested . This would be a cool enhancement, although I'm not sure how many other people have run into this. Just thought I would throw this out there. Thanks all.
Afrow UK#
You are giving ${MyArray->Shift} "VarA" and not $VarA, hence why you're getting "VarA" returned because that is what you put in the array!!

Also you can use the ReadFirst and ReadNext macros to loop through the array which makes it tidier.

-Stu
Jnuw#
Hey Stu. Yeah, I tried ${MyArray->Shift} "$VarA" too, no luck. The result of MessageBox MB_OK $1 after the Array Read is still "$VarA", and not "yes". Thanks for the suggestion though.

Jnuw
Afrow UK#
That is strange. You definitely have the variables defined with the Var instruction?

If not then would you be able to write a script that reproduces the problem.

-Stu
Jnuw#
I misspoke, but I still don't have it working. I tried: ${MyArray->Shift} "$$VarA". Needed the double $$, because I want the variable name in the Array, not the values, as they change over the course of the script. I have a working example of the problem below.

Basically, I want all the variable names to be stored in the Array, so I can loop through the array at any time, and see all the values of these variables. Make sense?

I have method 1 (my original), and method 2 ($$) included, thanks for the help.

!define ArrayNoValVar
!include "NSISArray.nsh"
Var VarA
Var VarB
Var VarC
Name Test
OutFile Test.exe
${Array} MyArray

Section ""
${MyArray->Init}
;Method 1
;${MyArray->Shift} "VarA"
;${MyArray->Shift} "VarB"
;${MyArray->Shift} "VarC"

;Method2
${MyArray->Shift} "$$VarA"
${MyArray->Shift} "$$VarB"
${MyArray->Shift} "$$VarC"

StrCpy $VarA yes
StrCpy $VarB no
StrCpy $VarC Yes

StrCpy $0 0
Loop:
IntCmp $0 3 Done 0 Done
${MyArray->Read} $1 $0

;Method1
;MessageBox MB_OK $$$1

;Method2
MessageBox MB_OK $1

IntOp $0 $0 + 1
Goto Loop
Done:
${MyArray->Delete}
${ArrayUnload}
SectionEnd
Afrow UK#
I see well that isn't possible. $VarName is for compile time only. Rather than have these different VarA-C variables, just use an Array.

-Stu
Jnuw#
Thanks Stu, will do. What I tried to do originally, is fill the Array with A, B, C, etc... and then as I looped through the Array, and set $1 to those elements, I would do something like this: MessageBox MB_OK $Var$1 , but that does not work either, and brought me back to my nested variables problem. Anyhow, I did as you suggested, and just stored these values in an array, and all is well. Thanks again.

Jnuw