Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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>
0
finnstone
Asked:
finnstone
  • 3
  • 3
  • 2
1 Solution
 
finnstoneAuthor Commented:
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
 
finnstoneAuthor Commented:
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
 
black0psCommented:
<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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
mmc98dl1Commented:
>> 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
 
finnstoneAuthor Commented:
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
 
black0psCommented:
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
 
mmc98dl1Commented:
Is that not the same as my answer a few posts ago?????
0
 
black0psCommented:
My code is #Evaluate(temptrack[i])#
Your code is #Evaluate("temptrack[i]")#

Those produce different outputs.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now