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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic varible names in loop 2

Status
Not open for further replies.

cumap

IS-IT--Management
Jul 9, 2007
268
US
How can I have dynamic variables that automatically increment with a loop they are in?

Here is what I mean:
Code:
For i=0 to 5
  x&i = 'blah'
Next

what is need is x1, x2, x3, x4, x5

Thanks!
 
Code:
<%
For i=0 to 5
  str  = "x" & i  & "= ""blah"""
    execute(str)
Next
%>

Your basically building the string:

x0 = "blah" and executing it for each itteration.

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Thank vicvirk,

Well, this is what I came up from your example
Code:
dcount=0
Do while not rs.eof
  str  = "ipAddr" & dcount  & "=" & trim(rs("ipAddress")) & ""
  execute(str)
  dcount = dcount+1
rs.movenext
Loop
and I'm getting this error
Expected end of statement at the line execute(str)

I have not try this Execute before so I'll be doing some research as soon as I finish this question. But if you can, please help clarify the error I'm having on the above.

Thanks!
 
Have you thought about using Arrays?

Ex:

Code:
dcount=0
Do while not rs.eof
  Redim Preserve ipAddr(dcount)
  ipAddr(dcount) = trim(rs("ipAddress"))

  dcount = dcount+1
rs.movenext
Loop

With an array, you can see how many elements are in it with....

uBound(ipAddr) + 1

Arrays indexes start at 0, so you need to add 1 to the ubound to get the count.

You can access any element of the array like this...

Response.Write ipAddr(7)

That will display the contents of the 8th array element.

In my opinion, using arrays for this will be many times easier than trying to create separate variables for each one.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thank gmmastros,

I'm afraid that I cannot use array for this particular case (I'd thought of it before) due to the fact that I'll need to insert the variables to javascript script in loop by number of dcount when body onload. More like I probably don't know exactly how to do just that using array. :)
 
I'm sure there is a way, but I don't know anything about javascript. There is a javascript forum here. You could try asking there.


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thank again, and I'll surely contact the js forum when I have my questions straight because remember back I'd already asked a question about how to have js interact with asp array but I got no luck since many, at the time, believed it is impossible/unthinkable of. Sincerely, I had and still do have my doubt but, just like you, I know very little about js. :)
 
nd I'm getting this errorExpected end of statement at the line execute(str)

you're missing a few quotes - I know it's hard to read, but it works, I really do wish ASP had escape characters...

Code:
str  = "ipAddr" & dcount  & "=""" & trim(rs("ipAddress")) & """"

Basically, you are building a command within a string, so it will look like this for each itteration:

ipAddr1 = "123.456.789.111"

The way you had it, the server was trying to execute the following command:

ipAdd1 = 123.456.789.111 (which would cause an error)

Works like a charm, I use it quite often to create dynamic variables on the fly...

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Thank vicvirk,

Basically, I changed my tactic and stuck those ipAddr into an array of <input type=hidden id=idAddr<%=i%>> and have js called those hidden value accordingly with getElementById command, and it seems to work just fine, for now.

Still, thank you for your input and I believe your Execute(str) can be very handy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top