Solved

concatenate variable in a loop

Posted on 2013-10-28
5
347 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
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 55

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 52

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 52

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Writing lines of code from a form output in script within a results page 2 23
Syntax for query to update table 2 29
Why "Mobile First"? 5 20
Clear input text 15 8
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to dynamically set the form action using jQuery.

821 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