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>
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>
ASKER
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 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.
<CFSET SetVariable(temptrack[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>
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],"
</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>
ASKER
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>
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Your code is #Evaluate("temptrack[i]")#
Those produce different outputs.
ASKER
<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>