Alpha Video Training
Results 1 to 11 of 11

Thread: Script Variable in a Loop

  1. #1
    Member
    Real Name
    Bo Roche
    Join Date
    Mar 2013
    Posts
    10

    Default Script Variable in a Loop

    Does anyone know if it is possible to have a variable inside a variable name?
    I have a collection of variables from a dialogue script, call them %A1, %A2, %A3, etc.
    Inside my loop I want to have this %A# variable count up with each loop, so that loop 1 pulls %A1, loop 2 uses %A2 and so on.
    The loop works, so I don't need help with that, I'm trying to re-purpose it for something else and need this capability.
    Thanks in advance for any help.

  2. #2
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,047

    Default Re: Script Variable in a Loop

    Not really sure what your question is but as best as I can figure this should do what your asking.

    Code:
    DIM vVar1C as C
    DIM vVar2C as C
    
    vVar1C = "vVar2C"
    
    vVar2C = "Random value"
    
    ?vVar1C
    = "vVar2C"
    
    
    ?eval(vVar1C)
    = "Random value"
    
    ?eval("vVar2C")
    = "Random value"
    Andrew

  3. #3
    Member
    Real Name
    Bo Roche
    Join Date
    Mar 2013
    Posts
    10

    Default Re: Script Variable in a Loop

    Andrew - Thanks for the reply.

    Your code doesn't look like the Alpha 4v8 scripting language so I'm not sure how to use it.

    What I was looking to do was call a set of sequentially numbered variables populated with values in a in dialogue script. I'm writing a script for batch record creation. This sequential variables contain ID numbers that are already in the database. The loop creates a service record for each of these items using a set of fixed variables also populated in the dialog script (date, tech, etc.).

    What I end up with is a series of sequentially number variables with ID tag values in them.
    %ID1 426587
    %ID2 421853
    %ID3 863586

    The script can enter data for each of these IDs by looping, but I couldn't figure out a method for having the %ID variable be a variable itself. On the first pass it needs to be %ID1, second should be %ID2 and third should be %ID3.

    I'm looking for something that would make a character in a variable name a variable in itself, that way I can have....
    VARIABLES: %N=3, ID's are those listed above, %SVCTECH=BOB, %SVCDATE=08222013
    SCRIPT:
    {SET %X,"1"}
    {:A}
    {FIELDENTER "SERVICE->ID", (%ID(%X))}
    {FIELDENTER "SERVICE->DATE", %SVCDATE)}
    {FIELDENTER "SERVICE->TECH", %SVCTECH}
    {SET %X,VAL(%X)+1}
    {LOOP :A, %N}

    Thereby creating a service record for each ID number.

  4. #4
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,047

    Default Re: Script Variable in a Loop

    oof; sorry didn't see you had posted in the v4 forum only noticed the version 8 bit.
    Andrew

  5. #5
    Member
    Real Name
    Bo Roche
    Join Date
    Mar 2013
    Posts
    10

    Default Re: Script Variable in a Loop

    I wasn't able to find a way to accomplish this, but I was able to find a way around it.

    I saved all of the variables into one long string and then use the SUBSTR function to select the desired characters from the string (up to 256 characters). In my case this wasn't that complicated because each variable is a known number of characters, 6 in this case, so all I needed to do was make the starting character a variable.

    Dialogue Script provides %N for number of loops(and thereby variables) and all of the variables themselves (%VRB1, 2, 3, etc.).

    {SET %VRbALL,%VRB1+%VRB2+%VRB3+....+%VRB15}
    {SET %NVRB,"1"}
    {:A}
    {SET %VRB,(SUBSTR(%VRBALL,VAL(%NVRB),6))}
    {FIELDENTER "DATABASE->FIELD",%VRB,%VRB_ERR}
    {SET %NVRB,VAL(%NVRB)+6}
    {LOOP :A, %N}

    A note about using Variables inside Expressions - Section 23-10 in the A4v6 reference manual covered a problem I hit regarding the fact that when using a script variable in an expression it will always be treated as a character variable/string, ignoring how the variable may be treated outside of the statement. This means that if the expression is expecting a number, as the SUBSTR expression does for the starting character and number of characters to copy values, you must convert the variable to number with the VAL statement inside the expression even if you have already done so outside the statement. This cost me a few hours. {SET %VRB,(SUBSTR(%VRBALL,VAL(%NVRB),6))}

    I can envision several improvements on this script that aren't needed for my application but I will mention just in case they are useful for someone else.
    1) You could evaluate the contents of each variable (%VRB1, %VRB2, etc.) to determine which are not blank and determine the value of %N instead of relying on user entry.
    2) You could have a variable length variable, as we as a variable number of variables. (that was clear as mud, right?) If you evaluated each %VRB# variable with LEN(TRIM(%VRB#)) then you could adapt the same process described above to create and establish a string of characters detailing the length of each variable string. You could then use those values to calculate out the changing start point and number of characters in the SUBSTR statement. I'm not going to provide more detail on this as it's a lot of work that I don't need to do, but I hope that helps you if you have to try it out.

  6. #6
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Script Variable in a Loop

    Hi Bo,

    To do indirection in Alpha 4, you place the variable name within braces, as in {%varnam}

    Whatever value is in the string varname, will be executed as if it was alpha four script code located right there in the rest of the code.
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  7. #7
    Member
    Real Name
    Bo Roche
    Join Date
    Mar 2013
    Posts
    10

    Default Re: Script Variable in a Loop

    Ira,

    Thanks for the response.
    I've didn't have any luck getting this to work.
    Here's one variation of the test script that I created for this - it errors out.

    {SET·%TVAR1,1234}
    {SET·%TVAR2,5678}
    {SET·%NTEST,1}
    {SET·%TVAR,{%TVAR{%NTEST}}} <--- this is trying to set %TVAR to %TVAR1
    {SET·%NTEST,2}
    {SET·%TVAR,{%TVAR{%NTEST}}} <--- this is trying to set %TVAR to %TVAR2

    This sets %TVAR to "%TVAR{%NTEST}"

    I'm not clear on how, in your example I can modify the string (varname) with a variable.

  8. #8
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Script Variable in a Loop

    Hi Bo,

    Quote Originally Posted by BoRoche View Post
    Ira,

    Thanks for the response.
    I've didn't have any luck getting this to work.
    Here's one variation of the test script that I created for this - it errors out.

    {SET·%TVAR1,1234}
    {SET·%TVAR2,5678}
    {SET·%NTEST,1}
    {SET·%TVAR,{%TVAR{%NTEST}}} <--- this is trying to set %TVAR to %TVAR1
    {SET·%NTEST,2}
    {SET·%TVAR,{%TVAR{%NTEST}}} <--- this is trying to set %TVAR to %TVAR2

    This sets %TVAR to "%TVAR{%NTEST}"

    I'm not clear on how, in your example I can modify the string (varname) with a variable.
    I probably didn't clearly describe the method, however, the macro interpreter substitution does not work inside a command statement where it is interpreted as data, e.g. a {SET ...} command. It will work inside an {IF ...} statement
    so if you had
    {SET %var1,"{F2}"}
    {%var1}
    it will place {F2} where{%var1} is

    In an IF statement

    {SET %var1,"{F2}"}
    {IF %var2=%var3,{{%var1}ABC},{{MAINMENU}}}

    would do an {F2}ABC if true.

    Substitution for keys in an ONKEY and other type events, have a different issue. They place the keys into the keyboard buffer and are played back only when the keyboard gets control, which can be tricky to do right.

    Hope this helps.
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  9. #9
    Member
    Real Name
    Bo Roche
    Join Date
    Mar 2013
    Posts
    10

    Default Re: Script Variable in a Loop

    Ira - thank you again for your time.

    I don't think this addresses what I'm trying to do as I would still need to have a statement for each iteration of the variable.

    I have 15 variables entered in a dialogue script, %ID1, %ID2... %ID15.
    What I want to do is write a loop that cycles through all of these variables by calling a different one during each loop.

    In it's most basic form...
    {SET %N,15}
    {:A}
    {SET # from %ID#, to %N} <----- Bogus code just get convey the idea
    {FIELDENTER "DATABASE->FIELD",%ID#,%ID_ERR} <----- In this case the # of %ID# would count down from 15
    {LOOP :A, %N}

    In a modern database application I would place the 15 records in a temp table and code the script to run through all the records in the table, but I don't think that's an option here. That's effectively what I've done in the somewhat cumbersome method from my 8/26/13 post. I use the variables to create what is essentially a fixed width DBF inside a string variable and then parse out the data from it. It just seems that there would be some way to create a variable with a variable element inside of it or at least one that will count up or down through an array of variables with a common naming structure.

    Thanks again. I hope I'm missing something.

  10. #10
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Script Variable in a Loop

    Hi Bo,

    Quote Originally Posted by BoRoche View Post
    {SET %N,15}
    {:A}
    {SET # from %ID#, to %N} <----- Bogus code just get convey the idea
    {FIELDENTER "DATABASE->FIELD",%ID#,%ID_ERR} <----- In this case the # of %ID# would count down from 15
    {LOOP :A, %N}
    I haven't checked this code, but it should look something like

    {SET %N,15}
    {:A}
    {* this builds a string that looks like the desired fieldenter string *}
    {set %tmp,"{FIELDENTER "+chr(34)+"DATABASE->FIELD"+chr(34)+",%ID"+%N+",%ID_ERR}"}

    {* this executes the code in the string %tmp *}
    {%tmp}
    {LOOP :A, %N}
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  11. #11
    Member
    Real Name
    Bo Roche
    Join Date
    Mar 2013
    Posts
    10

    Default Re: Script Variable in a Loop

    10,000 points to Ira!

    Thank you, sir. That works. Your original statement - "Whatever value is in the string varname, will be executed as if it was alpha four script code located right there in the rest of the code." told me everything I needed to know, but I didn't know enough at the time to interpret it correctly. Thank you for your time an patience.

    For the record, here's the script I wrote to test this out.
    %TVAR# variables are the values I want to use to update the %VAR variable to a new value in each loop. In a real world example they would be populated by user input in a Dialogue Script that calls this Keystroke Script. I've included them in this Keystroke Script for simplicity's sake.
    %VAR is the variable used inside the loop that gets updated with the %TVAR# values. In a real script this could be used, after the update, for any function that you would normally use a variable for.
    %NVAR is a counter that increases by 1 wiht each loop. This is used to walk through the %TVAR# variables. This could be done with %N, but that counts down, which I didn't like.
    %TMP is the String Variable that contains the code that sets %VAR to a different %TVAR# variable with each loop.
    %N is for the number of loops

    {SET·%N,3}
    {SET·%TVAR1,123}
    {SET·%TVAR2,456}
    {SET·%TVAR3,789}
    {SET·%NVAR,1}
    {:A}
    {SET·%TMP,"{SET·%VAR,·%TVAR"+%NVAR+"}"}
    {%TMP}
    {SET·%NVAR,·VAL(%NVAR)+1}
    {LOOP·:A,%N}

    When you run this code in Debug mode you can see that %VAR will be "123" on loop 1, "456" on loop 2 and "789" at the end.

    Thanks again Ira!

Similar Threads

  1. Script Variable Issues
    By nicknails in forum Alpha Four Versions 7 and 8
    Replies: 1
    Last Post: 10-03-2007, 10:05 AM
  2. Interrupting a loop in a script
    By nicknails in forum Alpha Four Versions 7 and 8
    Replies: 11
    Last Post: 07-28-2007, 10:57 AM
  3. script loop quits running in application
    By Rokrz in forum Alpha Four Versions 7 and 8
    Replies: 2
    Last Post: 08-29-2006, 11:34 AM
  4. Stop endless loop script
    By Mike Wilson in forum Alpha Five Version 7
    Replies: 5
    Last Post: 06-10-2006, 05:35 PM
  5. Script with variable
    By richardurban in forum Alpha Five Version 4
    Replies: 5
    Last Post: 01-18-2001, 01:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •