2024-02-06 01:40 AM - last edited on 2024-09-26 01:21 PM by Doreena Deng
Hi,
I am wondering if it is possible to change the variable name on the fly, e.g when calling from a Subroutine, having the variable name part1+part2 merged together. An example would be a variable called Var_A where the prefix is added to the suffix, like Len = "Var"+"_A".
This would be great as it would allow a single subroutine to have its parameters changed easily, from _A to _C etc, especially when there are quite a few options variables and keep the code small. Is this possible or is there a better way of making a simple repeating subroutine with different variable inputs?
For a code example in 2D Script:
option1 = 1
option2 = 1
option3 = 1
Var_A = 0.010
Var_B = 0.015
Var_C = 0.020
IF option1 = 1 THEN
Suffix = "_A"
Len = "Var"+Suffix
Offset = 0.01
Gosub "CircleTest":
ENDIF
IF option2 = 1 THEN
Suffix = "_B"
Len = "Var"+Suffix
Gosub "CircleTest":
ENDIF
IF option3 = 1 THEN
Suffix = "_C"
Len = "Var"+Suffix
Gosub "CircleTest":
ENDIF
END
!*************************************
"CircleTest": !Gosub Reference
Circle2 0,0,Len
RETURN
Ideally this should return three circles, but it comes up with an error.
Is there a way to string together the suffix to a common prefix?
Many thanks, Matt
Solved! Go to Solution.
2024-02-06 02:02 AM - last edited on 2024-02-06 04:27 AM by Laszlo Nagy
No you can not change variable names, there should be no need to.
All you need in this case is ...
IF option1 = 1 THEN
Len = 0.010
Offset = 0.01
Gosub "CircleTest":
ENDIF
IF option2 = 1 THEN
Len = 0.015
Gosub "CircleTest":
ENDIF
IF option3 = 1 THEN
Len = 0.020
Gosub "CircleTest":
ENDIF
I don't quite get what you are trying to do with 'option_1', 'option_2' & 'option_3' parameters, unless they are 3 separate boolean (on/off0 parameters.
So you could potentially have all 3 circles on at the same time.
If you simply want a choice of option 1, 2 or 3, then create a integer parameter called 'Option' (without the quotes) and give it a VALUES list in the parameter script.
VALUES "Options" 1, 2, 3
Then you will be able to choose only one of the circles at any one time.
Barry.
2024-02-07 02:33 AM
...
Len_ = Len_A
goSUB
...
Len_ = Len_B
goSUB
...
Pretty sure what you were trying turns your parameter into a text type so that probably explains why it fails.
You could also play with matrices.
DIM Len[]
Len[1] = 0.2
Len[2] = 0.33
...
Suffix = 1
goSUB
...
CIRCLE2 = 0, 0, Len[Suffix]
Ling.
AC22-28 AUS 3110 | Help Those Help You - Add a Signature |
Self-taught, bend it till it breaks | Creating a Thread |
Win11 | i9 10850K | 64GB | RX6600 | Win11 | R5 2600 | 16GB | GTX1660 |
2024-02-06 02:02 AM - last edited on 2024-02-06 04:27 AM by Laszlo Nagy
No you can not change variable names, there should be no need to.
All you need in this case is ...
IF option1 = 1 THEN
Len = 0.010
Offset = 0.01
Gosub "CircleTest":
ENDIF
IF option2 = 1 THEN
Len = 0.015
Gosub "CircleTest":
ENDIF
IF option3 = 1 THEN
Len = 0.020
Gosub "CircleTest":
ENDIF
I don't quite get what you are trying to do with 'option_1', 'option_2' & 'option_3' parameters, unless they are 3 separate boolean (on/off0 parameters.
So you could potentially have all 3 circles on at the same time.
If you simply want a choice of option 1, 2 or 3, then create a integer parameter called 'Option' (without the quotes) and give it a VALUES list in the parameter script.
VALUES "Options" 1, 2, 3
Then you will be able to choose only one of the circles at any one time.
Barry.
2024-02-06 02:31 AM
Hi Barry,
Thank you so much for responding! I am trying to improve my coding skills. I see experts creating objects that are very concise in code, using subroutines. It seems pretty neat to be able to code so cleanly. I wondered if I could do it with changing variable names dynamically.
That is my answer "No you can not change variable names". Thank you again Barry and all the best, Matt
2024-02-06 02:49 AM
The question is, why is there a need to change the variable name? In your example, it is redundant, hence Barry's answer. Does it not achieve what you are wanting? Is there something else you are trying to achieve?
Ling.
AC22-28 AUS 3110 | Help Those Help You - Add a Signature |
Self-taught, bend it till it breaks | Creating a Thread |
Win11 | i9 10850K | 64GB | RX6600 | Win11 | R5 2600 | 16GB | GTX1660 |
2024-02-06 11:02 PM - last edited on 2024-02-11 01:33 AM by Laszlo Nagy
Hi Ling,
Thank you for responding, I have seen some of your work and you are a true GDL coding artist! It would a dream to have your and Barry's level of coding skills.
My thought stemmed from reading AC GDL objects and how clean the script was written. I have found what would take me 50 lines of code, experts could write in 20 lines. Much neater and cleaner to follow. So my thinking became, how can I, without going back to university to get a programming degree, make my script concise and clean. Along those lines, I wondered if I could somehow reuse Subroutines multiple times, instead of writing the same repeating code but with variations in the variable names. An example of the logic being:
ValueListParameter 1 to 20, each with say 20 variables A to T. For that I code:
IF ValueListParameter1=1 THEN
Len_ = 0.2
Ht_=0.33
Wdt_A=25
!...etc
Gosub "Create Object:"
ENDIF
...
IF ValueListParameter20=1 THEN
Len_ = 0.4
Ht_=1.33
Wdt_=58
!...etc
Gosub "Create Object:"
ENDIF
END
***********
"Create Object":
Len = Len_
Ht = Ht_
Wdt = Wdt_
!...etc
RETURN
The above 20 variables have suffixes A through to T say with unique values. In the Subroutine, if I could just change the variable suffix based upon the Gosub. Then the variables could all be stored cleanly at the start of the scipt, ready to be called and more easily managed.
If I could write something like this:
!List of Variable Values to lookup:
Len_A = 0.2
Len_B=0.33
...
Len_T=0.43
Ht_A=58
...
Ht_T=58
Wdt_A=22
...
Wdt_T=32
!...etc
IF ValueListParameter1=1 THEN
Suffix="A"
Gosub "Create Object":
ENDIF
...
IF ValueListParameter20=1 THEN
Suffix="T"
Gosub "Create Object":
ENDIF
END
***********
"Create Object":
Len = "Len_" + Suffix
Ht = "Ht_" + Suffix
Wdt = "Wdt_" + Suffix
!...etc
RETURN
Welcome any and all thoughts and many thanks, Matt
2024-02-07 02:33 AM
...
Len_ = Len_A
goSUB
...
Len_ = Len_B
goSUB
...
Pretty sure what you were trying turns your parameter into a text type so that probably explains why it fails.
You could also play with matrices.
DIM Len[]
Len[1] = 0.2
Len[2] = 0.33
...
Suffix = 1
goSUB
...
CIRCLE2 = 0, 0, Len[Suffix]
Ling.
AC22-28 AUS 3110 | Help Those Help You - Add a Signature |
Self-taught, bend it till it breaks | Creating a Thread |
Win11 | i9 10850K | 64GB | RX6600 | Win11 | R5 2600 | 16GB | GTX1660 |
2024-02-07 03:09 AM
Wow that is a good idea! DIM could be the way to go, I will see how I go coding, clean scripts coming up. Thank you so much Ling! Matt
2024-02-07 05:28 AM
If you really wanted to, you could merge your Length (x), Width (y) and Height (z) parameters into a single two dimensional array.
AC22-28 AUS 3110 | Help Those Help You - Add a Signature |
Self-taught, bend it till it breaks | Creating a Thread |
Win11 | i9 10850K | 64GB | RX6600 | Win11 | R5 2600 | 16GB | GTX1660 |
2024-02-07 12:53 PM
There are a few cases in which variables can be composed of several parts.
Example:
_wall_scale = GLOB_SCALE
var = "Wall_" + STR(_wall_scale, 1, 0)
MATERIAL 1
GOSUB var
! ---------------------------- E N D ----------------------------------- !
! ---------------------------- E N D ----------------------------------- !
END ! -- END -- END -- END -- END -- END -- END -- END -- END -- END -- !
! ---------------------------- E N D ----------------------------------- !
! ---------------------------- E N D ----------------------------------- !
"Wall_200":
BLOCK 1, 1, 1
RETURN
! ---------------------------------------------------------------------- !
! ---------------------------------------------------------------------- !
"Wall_100":
BLOCK 1, 2, 3
RETURN
! ---------------------------------------------------------------------- !
! ---------------------------------------------------------------------- !
"Wall_50":
BLOCK 1, 4, 9
RETURN
! ---------------------------------------------------------------------- !
! ---------------------------------------------------------------------- !
2024-02-08 04:42 AM
The difference is that GOSUB is expecting a text string, which is what var is being defined as, whilst CIRCLE2 is expecting a numeric string or numberic parameter?
AC22-28 AUS 3110 | Help Those Help You - Add a Signature |
Self-taught, bend it till it breaks | Creating a Thread |
Win11 | i9 10850K | 64GB | RX6600 | Win11 | R5 2600 | 16GB | GTX1660 |