Solved

concatenate variable in a loop

Posted on 2013-10-28
5
344 Views
Last Modified: 2013-10-28
I have a my variables set up to be prz1, prz2, prz3.
Then  I want to loop through a for and display all the text out on new lines.
for i = 1 to 30
	response.write(prz1) 
next

Open in new window


how do i change the variable to be prz & i, since this simple syntax doesn't work?
0
Comment
Question by:rivkamak
  • 2
  • 2
5 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
Comment Utility
First, use an array instead of individual variables:

Dim PRZ(1 To 20) As Variant
PRZ(1) = 1
PRZ(2) = 23
...

Then concatenation is easy:

Response.Write Join(PRZ, vbCRLF)

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
If you want all the variable values on one line separated by commas and spaces:

Response.Write Join(PRZ, ", ")

Kevin
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
If I understand you correctly you want to be able to reference your variables as przi where the i is the loop value

This you can't do - you would have to store your variables in an array as suggested by zorvek.

In other scripting languages it is possible to create references to variables dynamically but as far as I am aware ASP does not support this.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
 
strPrizes="prz1, prz2, prz3"
arrPrizes=split(strPrizes,",")

For each prize in arrPrizes
    response.write prize &"<br>"
next

response.write "<ul>"
For each prize2 in arrPrizes
        response.write "<li>"&prize2&"</li>
next
response.write "</ul>

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
If you needed to get the array dynamically you can use a dictionary.

Dim dPrize,counter
counter=0
Set dPrize=Server.CreateObject("Scripting.Dictionary")

strPrizes="prz1, prz2, prz3"
arrPrizes=split(strPrizes,",")

For each prize in arrPrizes
    counter=counter+1
    dPrize.Add counter,prize
next

response.write dPrize.Item(2) ' should be prz2

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now