Veronica
Veronica

Reputation: 23

Using Loop List in CF to create combinations

I've received a lot of help on stackoverflow and I greatly appreciate it. I seem to be stuck on coding this List Loop correctly. I'm aware there are much simpler ways to code this project, but my student project REQUIRES me to pass variables through the URL. I am trying to simply combine the passwords I'm passing through the URL to create all password combos available with six values (cold,fusion,dynamic and bert, ernie, oscar). I've isolated the issue to my List Loop. Can you guys tell what I'm missing here? Thanks in advance.

Error Message:

Error casting an object of type coldfusion.compiler.ASTstructureReference cannot be cast to java.lang.String to an incompatible type.

This usually indicates a programming error in Java, although it could also mean you have tried to use a foreign object in a different way than it was designed.

passwords.cfm:

<cfinclude template="header.cfm">
<body>

<h2>Loop List</h2>

<a href="looplist.cfm?List1=cold,fusion,dynamic&List2=bert,ernie,oscar"> 
Click here for all password combinations</a>

<cfinclude template="footer.cfm">

looplist.cfm:

<cfinclude template="header.cfm">

<h2>Loop List</h2>

<cfloop Index = "#URL.List1#" List = "#URL.List2#">
    <cfloop Index = "#URL.List2#" List = "#URL.List1#">
    </cfloop>
</cfloop>

<cfset passwordList= #URL.List1# & #URL.List2#>

<UL><cfoutput>#passwordList#</cfoutput><UL><BR>

<cfinclude template="footer.cfm">

Upvotes: 1

Views: 280

Answers (2)

Alex Baban
Alex Baban

Reputation: 11732

Start with an empty password list.

You then have the outer loop (index i), from where you take the left side of the combined word.

From the inner loop (index j) you take the right side of the combined word. Also here (the inner loop) you build a second combination (switch the right side and left side words), then add the two combinations to the 'password list'.

  <cfset passwordList = "" />

  <cfloop index="i" list="#URL.List1#">

    <cfset tempPasswordCombo1 = "" />
    <cfset tempPasswordCombo2 = "" />

    <cfloop index="j" list="#URL.List2#">
      <cfset tempPasswordCombo1 = i & j />
      <cfset tempPasswordCombo2 = j & i />      
      <cfset passwordList = listAppend(passwordList, tempPasswordCombo1) />
      <cfset passwordList = listAppend(passwordList, tempPasswordCombo2) />
    </cfloop>

  </cfloop>


  <cfoutput>#passwordList#</cfoutput>

Upvotes: 0

Leigh
Leigh

Reputation: 28873

<cfloop Index = "#URL.List1#" ...>

The "index" should be a simple string containing the name of a variable, such as "MyVariable". The pound signs, around URL.List1, force that variable to be evaluated. So you are actually passing in its value as the name, ie "cold,fusion,dynamic". Since that is not a valid variable name, that is what is causing the cryptic compile error you are seeing.

Given that this is homework, I am not going to write the code for you, but rather provide an example you can build upon. Like I suggested in the comments of your other thread

  • Start simply. To make things easier, you can temporarily hard code the URL parameters. Use cfdump and cfoutput frequently to display variables at different points to better understand what the code is doing.

  • Do not use List1 for both the loop "index" and url variable. Use two different variable names.

Starter Example:

<!--- Hard code values for testing ONLY --->
<cfset URL.List1 = "cold,fusion,dynamic">
<cfset URL.List2 = "bert,ernie,oscar">

<cfloop List="#URL.List2#" index="OuterValue">
    <!--- Display current element in outer loop for debugging only --->
    <cfoutput>
        <h3>OUTER LOOP: Current element from URL.List2 is:  #OuterValue#</h3>
    </cfoutput>

    <cfloop List = "#URL.List1#" index="InnerValue">
        <!--- Display current element in inner loop for debugging only --->
        <cfoutput>
            INNER LOOP: Current value from URL.List1 is: #InnerValue#<br>
        </cfoutput>

         <!--- 
            ... real code that does something with the two variables here .... 
         --->
    </cfloop>
</cfloop>

Upvotes: 2

Related Questions