Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamicaly asign variable names from an array

Status
Not open for further replies.

bebblebrox

IS-IT--Management
Aug 9, 2004
39
0
0
US
Hi,

I have an array with two cells. The first cell is the name of a variable and the 2nd cell has the value that i'd like to assign to that variable. How can I do this?

Thanks,

RP


 
Where are you getting your data and what do you want to do with it?

If you are getting it from either the browser Request or a Recordset it may be a simple issue of transferring data from one structure to the other.

But then that raises the questions of WHY?
 
sheco is correct, where are you getting this from?

but just so we sort of answer your question, you can try a dictionary object

Code:
Set d=Server.CreateObject("Scripting.Dictionary")
d.Add "Name","yourvalue"

response.Write d.Item("Name")

Set d=nothing
 

Here's some code, but don't think this is your answer...

Code:
dim myArr(1,1)

myArr(0,0) = "var1"
myArr(0,1) = "value1"
myArr(1,0) = "var2"
myArr(1,1) = "value2"

for i = lbound(myArr,1) to ubound(myArr,1)
  execute (myArr(i,0) & " = " & myArr(i,1))
next

response.write(var1)
response.write(var2)

That may be, but.... why not just access the data from the array directly... the above seems a waste of time ? Also you should be aware that executing values from an untrusted source... i.e. the user (GET or POST) is a security hole, be very cautious of executing strings as code, even from a trusted source, these could cause you problems.

Sheco raises the important question that you need to answer: WHY ?



A smile is worth a thousand kind words. So smile, it's easy! :)
 

oops, missed a line:

Code:
dim myArr(1,1)

myArr(0,0) = "var1"
myArr(0,1) = "value1"
myArr(1,0) = "var2"
myArr(1,1) = "value2"

for i = lbound(myArr,1) to ubound(myArr,1)
  execute ("dim " & myArr(i,0))
  execute (myArr(i,0) & " = " & myArr(i,1))
next

response.write(var1)
response.write(var2)


A smile is worth a thousand kind words. So smile, it's easy! :)
 
the reason:

i have a search page with ~50 criteria. when someone searchs i'm taking the entire page and storing it in a cookie with delimited fieldname/value pairs.

when someone returns to search page, i can pull the cookie value and split it out into an array. then i can access the name/value pair and put the user's criteria back into the correct text box.

 
i would use a dictonary object in a function like this

Code:
function setdict(d,dname,dvalue)

Set d = server.CreateObject("Scripting.Dictionary")
dname=split(dname,",")
dvalue=split(dvalue,",")
for i=lbound(dname) to ubound(dname)
d.Add dname(i), dvalue(i)
next

end function

call it like this, by have 1 comma sep for the names and 1 for the values

Code:
setdict dc,"name1,name2","hello,world"

then you display it like this
Code:
response.write dc.item("name2") ' will display world
 
you can also make it into an array of dictionary objects

Code:
<%
dim d(5)
for i=0 to 5
Set d(i)=Server.CreateObject("Scripting.Dictionary")
d(i).Add "Name","Value "&(i)
next

for i=0 to 5
response.Write d(i).Item("Name") & "<br>"
next
%>
 
IMHO is best never to use execute in conjunction with user input... unless you really trust your users AND you really trust your ability to filter malicious code.
 
I would agree - far too risky, it's almost like giving them write access to your scripting environment.



A smile is worth a thousand kind words. So smile, it's easy! :)
 

Based on the additional info about the requirement, so would I ! ;-)

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top