Search code examples
vbscriptasp-classic

Fetching a variable from a vbscript array


I have these variables

STATE_Newverifdetaillibelle_208="test"   
STATE_Newverifdetaillibelle_209=""    
STATE_Newverifdetaillibelle_210=""    
STATE_Newverifdetaillibelle_211=""    
STATE_Newverifdetaillibelle_212=""    
STATE_Newverifdetaillibelle_213=""    
STATE_Newverifdetaillibelle_214=""    
STATE_verifmoduleid="208, 209, 210, 211, 212, 213, 214"

So i would like to fetch the variable which is not empty based on the list/array

In my example, STATE_verifmoduleid contains the following:

STATE_verifmoduleid="208, 209, 210, 211, 212, 213, 214"

So basically, I should catch only STATE_Newverifdetaillibelle_208="test" because it's not empty.

I wrote this code but it's not returning the value "TEST" contained in the variable STATE_Newverifdetaillibelle_208="test"

Dim STATE_Newverifdetaillibelle_(1000)
Dim verifmoduleid

'Here the code of the form/request.form, bla bla bla which is working ...

Dim CheckVerifyModulesIDs,CheckVerifyModulesID, Citems

CheckVerifyModulesIDs = replace(STATE_verifmoduleid," ","")
CheckVerifyModulesID = Split(CheckVerifyModulesIDs,",")

For each Citems in CheckVerifyModulesID
    if (STATE_Newverifdetaillibelle_(Citems)) <> "" then
        response.write (STATE_Newverifdetaillibelle_(Citems)) & "<br>"
    end if
next

How can I fix it?


Solution

  • All the variables STATE_Newverifdetaillibelle_* are declared separately, so you need to build each variable name dynamically as a string and evaluate that string as expression.

    STATE_Newverifdetaillibelle_208 = "test"   
    STATE_Newverifdetaillibelle_209 = ""    
    STATE_Newverifdetaillibelle_210 = ""    
    STATE_Newverifdetaillibelle_211 = ""    
    STATE_Newverifdetaillibelle_212 = ""    
    STATE_Newverifdetaillibelle_213 = ""    
    STATE_Newverifdetaillibelle_214 = ""    
    STATE_verifmoduleid = "208, 209, 210, 211, 212, 213, 214"
    
    ' Here the code of the form/request.form, bla bla bla which is working ...
    
    ids  =  Split(Replace(STATE_verifmoduleid," ",""),",")
    
    For Each id in ids
        v = Eval("STATE_Newverifdetaillibelle_" & id)
        If v <> "" then
            Response.Write v & "<br>"
        End If
    Next
    

    Another way is to put all values to an array or a dictionary, then there is no need to use Eval.