Link to home
Start Free TrialLog in
Avatar of finnstone
finnstone

asked on

looping and using variables to call other variables

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>
Avatar of finnstone
finnstone

ASKER

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>
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>
<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.
>> 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>
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>


ASKER CERTIFIED SOLUTION
Avatar of black0ps
black0ps
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is that not the same as my answer a few posts ago?????
My code is #Evaluate(temptrack[i])#
Your code is #Evaluate("temptrack[i]")#

Those produce different outputs.