?
Solved

looping and using variables to call other variables

Posted on 2005-03-17
8
Medium Priority
?
202 Views
Last Modified: 2013-12-24
this code does not work, can you help, i want to reference variables using other variables..

as you can see i am trying to reference temptrack[1], temptrack[2], temptrack[3],  as the variable temptrack[#i#]  that was defined earlier....i have to do it this way because i in my real code i will not know if there are 5 , 6 ,7, 8 .. records


<cfloop index = "i" from = "1" to = "5">
<CFSET temptrack[#i#] = "label"&#i# >

</cfloop>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body><cfoutput>
<cfloop index = "i" from = "1" to = "5">
<cfset stringtemp = "temptrack">
<cfset stringtemp2 = #i#>
UPS Tracking Number: ##stringtemp#stringtemp2##
</cfloop>
</cfoutput>
</body>
</html>
0
Comment
Question by:finnstone
[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
  • 3
  • 3
  • 2
8 Comments
 

Author Comment

by:finnstone
ID: 13568887
i tried to this too just changed line 2

<cfloop index = "i" from = "1" to = "5">
<CFSET temptrack[i] = "label"&#i# >

</cfloop>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body><cfoutput>
<cfloop index = "i" from = "1" to = "5">
<cfset stringtemp = "temptrack">
<cfset stringtemp2 = #i#>
UPS Tracking Number: ##stringtemp#stringtemp2##
</cfloop>
</cfoutput>
</body>
</html>
0
 

Author Comment

by:finnstone
ID: 13568906
would this work??

<cfloop index = "i" from = "1" to = "5">
<CFSET temptrack[i] = "label"&#i# >

</cfloop>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body><cfoutput>
<cfloop index = "i" from = "1" to = "5">
#temptrack[i]#
</cfloop>
</cfoutput>
</body>
</html>
0
 
LVL 7

Expert Comment

by:black0ps
ID: 13569075
<cfloop index = "i" from = "1" to = "5">
<CFSET SetVariable(temptrack[i],"label"&i) >
</cfloop>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body><cfoutput>
<cfloop index = "i" from = "1" to = "5">
#Evaluate("temptrack[i]")#
</cfloop>
</cfoutput>
</body>
</html>

Give that a try.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 12

Expert Comment

by:mmc98dl1
ID: 13570320
>> i have to do it this way because i in my real code i will not know if there are 5 , 6 ,7, 8 .. records

You should loop from 1 to the length of the array to avoid errors:

<cfloop index = "i" from = "1" to = "5">
<CFSET SetVariable(temptrack[i],"label"&i) >
</cfloop>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body><cfoutput>
<cfloop index = "i" from = "1" to = "#ArrayLen(temptrack)#">
#Evaluate("temptrack[i]")#
</cfloop>
</cfoutput>
</body>
</html>
0
 

Author Comment

by:finnstone
ID: 13571571
actually can you confirm if this would work, and tell me why if not - it looks like this works even though it is so simple!!

<cfloop index = "i" from = "1" to = "5">
<CFSET temptrack[i] = "label"&#i# >

</cfloop>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body><cfoutput>
<cfloop index = "i" from = "1" to = "5">
#temptrack[i]#
</cfloop>
</cfoutput>
</body>
</html>


0
 
LVL 7

Accepted Solution

by:
black0ps earned 360 total points
ID: 13575498
Doesn't temptrack[i] have the name of your variable you want to call? If it does, the output:
<cfloop index = "i" from = "1" to = "5">
#temptrack[i]#
</cfloop>
would output all five of your variables.

Try:
<cfloop index = "i" from = "1" to = "5">
#Evaluate(temptrack[i])#
</cfloop>
0
 
LVL 12

Expert Comment

by:mmc98dl1
ID: 13628114
Is that not the same as my answer a few posts ago?????
0
 
LVL 7

Expert Comment

by:black0ps
ID: 13644129
My code is #Evaluate(temptrack[i])#
Your code is #Evaluate("temptrack[i]")#

Those produce different outputs.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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