Monday 26 March 2012

ASP: dynamic variable names

   


I've been programming in ASP for such a long time that sometimes I do not really value its power. Today we are going to see an extreme example: dynamic variable names. I say "extreme" because I believe that the real use of the following code might be quite rare, however in some extreme situations, we need extreme - I know I repeat myself - solutions.
We are going to use different ASP stuff: an array and the execute and eval functions. At the same time we are going to use the split function, in order to create the array.

Some assumptions
The following example starts from a precise idea. Imagine we have a variable that contains different values. Those values are separated by a semicolon. Something like:
TheString = "01;02;03;04;05"
The above variable is changing depending on - for example - the recordset we retrieve from a table. Thus, TheString will not contain the same semicolon separated values. And not even the same number of items.
We need to create a certain number of variables depending on the values contained in TheString and assign those values to different variables. In short, we need to obtain something like:
a1 = "01"
a2 = "02"
a3 = "03"
a4 = "04"
a5 = "05"
There's no real relation between each variable: the fact that the variable name is a1 has no relation to the fact that the assigned value is "01".
At the same time, we do not know beforehand the number of variables we will need, because we don't know the number of values in TheString.

The array
The first thing we need is to create the array. To do so, we split TheString considering the semicolon as separator.
<%
Dim TheArray
Dim TheString
TheString = "01;02;03;04;05"
TheArray = split(TheString, ";")
Then we need to assign the items in the array to dynamic variables. Remember we do not know how many items are stored in the array, and thus we do not know how many variables we are going to create.

Items and variables
We use a For... Next to go through every item in the array. At the same time, for the purpose of this example, we are showing the items values with response.write:
Dim i
i = 1
For each item in TheArray
response.write "<br>Item Value: " & item
The i variable is used in the next bit of the code: we will append the i value to an "a" for the dynamic variable names:
execute("a" & i & " = item")
i = i + 1
Next
As you can see, the i value is increasing every time a new variable is created. When the For... Next cycle is finished, we end up with i having a value corresponding to the number of items in the array plus one. We need to remember that in the next step. Notice that we use the execute function to create the variables.

Show the dynamic variables
As said, we don't know how many variables we created: it depends on the number of items in the array. Anyway, we can cycle throught those variables using an approach similar to the above. The number of variables is i minus 1. We then use j for a new For... Next cycle:
i = i - 1
Dim j
For j=1 to i
response.write "<br>a" & j &": " & eval("a" & j)
Next
%>
The complete code will generate the following result (given TheString="01;02;03;04;05"):
Item Value: 01
Item Value: 02
Item Value: 03
Item Value: 04
Item Value: 05
a1: 01
a2: 02
a3: 03
a4: 04
a5: 05

And that is all. Let me know if you've found the above information useful!

3 comments:

  1. Thank You, I have looked around just to fix this so it was very helpful thankyou
    Peter - Sweden

    ReplyDelete
  2. This is a good example on creating dynamic variables in classic ASP. Believe it or not, I am still using classic ASP for one of my old projects.

    ReplyDelete
  3. it still using asp in 2018 and have new projects for 2019, it rocks, asp + bootstrap + jquery is the king, dyanmic, powerful, faster

    ReplyDelete

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.