Solved

concatenate variable in a loop

Posted on 2013-10-28
5
350 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 58

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

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
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…

623 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