Solved

concatenate variable in a loop

Posted on 2013-10-28
5
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 57

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 53

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 53

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
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 …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

751 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