2017-03-11 38 views
1

我已經收到了很多關於stackoverflow的幫助,我非常感謝。我似乎被困在編碼這個List Loop正確。我知道有很多簡單的方法來編寫這個項目,但我的學生項目需要我通過URL傳遞變量。我試圖簡單地將我通過URL傳遞的密碼合併,以創建所有可用六個值(cold,fusion,dynamic and bert, ernie, oscar)提供的密碼組合。我已將問題隔離到我的List Loop。你們能告訴我在這裏錯過了什麼嗎?提前致謝。在CF中使用循環列表來創建組合

錯誤消息:

錯誤鑄造 類型coldfusion.compiler.ASTstructureReference不能轉換到 java.lang.String中到不兼容的類型的一個對象。

這通常表示在Java中有編程錯誤 ,但它也可能意味着您已嘗試使用 以與設計不同的方式使用外部對象。

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"> 
+1

你「再利用」 as'index'of循環您的網址變量和覆蓋變量... –

+0

你能對你的意思是「什麼樣的詳細說明問題」?代碼是否引發錯誤?如果是,請編輯您的問題以包含實際的錯誤消息。 – Leigh

+0

好吧,所以基本上我需要重新命名我的索引值? – Veronica

回答

0

開始使用空密碼列表。

然後你有外部循環(索引我),你從哪裏拿走組合詞的左側。

從內部循環(索引j)中取出組合詞的右側。在這裏(內部循環),您還可以創建第二個組合(切換右側和左側單詞),然後將這兩個組合添加到「密碼列表」中。

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

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

「索引」 應該是包含可變的名稱一個簡單的字符串,如 「MyVariable的」。英鎊標誌,大約在URL.List1,強制該變量進行評估。所以你實際上傳遞了它的作爲名字,即"cold,fusion,dynamic"。由於這不是valid variable name,那是什麼導致你看到的神祕的編譯錯誤。

鑑於這是家庭作業,我不打算爲您編寫代碼,而是提供一個可以構建的示例。就像我在the comments of your other thread中提出的

  • 開始簡單。爲了簡化操作,您可以臨時對URL參數進行硬編碼。經常使用cfdumpcfoutput來顯示不同點的變量,以更好地理解代碼正在做什麼。

  • 請勿將List1用於循環「index」和url變量。使用兩個不同的變量名稱。

入門實例:

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