Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

concatenate variable in a loop

Posted on 2013-10-28
5
Medium Priority
?
352 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 2000 total points
ID: 39606947
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)
ID: 39606949
If you want all the variable values on one line separated by commas and spaces:

Response.Write Join(PRZ, ", ")

Kevin
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39606974
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 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39606997
 
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 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39607016
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

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