不能用 ColdFusion 中的结构做数学运算
Can not do math with structures in ColdFusion
我正在遍历一个列表,如果结构键不存在,我想创建一个结构的结构,但如果它存在,我只想通过添加一个新值来更新其中一个键该键的现有值。它不会那样做,我不知道为什么。
代码如下:
<cfloop list="#recipeItemList#" index="ril">
<cfset thisrecipe = entityLoadByPK("recipes", #ril#)>
<cfloop array="#thisrecipe.getrecipeitem()#" index="recipeItems">
<cfif recipeItems.hasrawMaterial_id()>
<cfset theitem = entityLoadByPK("rawMaterials", recipeItems.getrawMaterial_id().getid())>
<cfinvoke method="getPrepByDate" component="/FEcomponents.production" returnvariable="makeAmount">
<cfinvokeargument name="recipe_id" value="#ril#">
<cfinvokeargument name="delivery_date" value="#delivery_date#">
</cfinvoke>
<cfset thisRecipeYield = #thisRecipe.getYield()#>
<cfif thisRecipeYield neq 0>
<cfset recipeRatio = makeAmount / thisRecipeYield>
</cfif>
<cfset thisQuantity = recipeRatio * #recipeItems.getitemQuantity()#>
<cfif structKeyExists(outerstruct, '#theitem.getid()#')>
<!--- we must add this quantity to the existing key, not create a new one --->
<cfset addthis = #outerstruct[theitem.getid()].totalToGet# + #thisQuantity#>
<cfset structInsert(outerstruct[theitem.getid()],'totalToGet', #addthis#,true)>
<!--- this is just to for testing to see if the value has changed, and it does not the 'addthis' value is always the latest value in the loop--->
#outerstruct[theitem.getid()].totalToGet# + #thisquantity# = #addthis# <BR>
<cfelse>
<!--- create a new struct --->
<cfset outerstruct[theitem.getid()] = structnew()>
<cfset outerstruct[theitem.getid()].rawMaterialid = #theitem.getid()#>
<cfset outerstruct[theitem.getid()].rawMaterialName = #theItem.getName()#>
<cfset outerstruct[theitem.getid()].totalToGet = #thisQuantity#>
<cfset outerstruct[theitem.getid()].uom = #theItem.getUOM().getname()#>
</cfif>
</cfif>
</cfloop>
</cfloop>
我哪里做错了。
感谢您的帮助
我认为这会满足您的要求,我删除了一些额外的井号并更改了您更新结构键的方式。抱歉,我无法 运行 代码,但我相信这应该可行。
<cfloop list="#recipeItemList#" index="ril">
<cfset thisrecipe = entityLoadByPK("recipes", #ril#)>
<cfloop array="#thisrecipe.getrecipeitem()#" index="recipeItems">
<cfif recipeItems.hasrawMaterial_id()>
<cfset theitem = entityLoadByPK("rawMaterials", recipeItems.getrawMaterial_id().getid())>
<cfinvoke method="getPrepByDate" component="/FEcomponents.production" returnvariable="makeAmount">
<cfinvokeargument name="recipe_id" value="#ril#">
<cfinvokeargument name="delivery_date" value="#delivery_date#">
</cfinvoke>
<cfset thisRecipeYield = thisRecipe.getYield()>
<cfset thisQuantity = recipeItems.getitemQuantity()>
<cfif thisRecipeYield neq 0>
<cfset recipeRatio = makeAmount / thisRecipeYield>
<cfset thisQuantity *= recipeRatio>
</cfif>
<cfif structKeyExists(outerstruct, theitem.getid())>
<!--- we must add this quantity to the existing key, not create a new one --->
<cfset outerstruct[theitem.getid()].totalToGet += thisQuantity>
<cfelse>
<!--- create a new struct --->
<cfset outerstruct[theitem.getid()] = structnew()>
<cfset outerstruct[theitem.getid()].rawMaterialid = theitem.getid()>
<cfset outerstruct[theitem.getid()].rawMaterialName = theItem.getName()>
<cfset outerstruct[theitem.getid()].totalToGet = thisQuantity>
<cfset outerstruct[theitem.getid()].uom = theItem.getUOM().getname()>
</cfif>
</cfif>
</cfloop>
我正在遍历一个列表,如果结构键不存在,我想创建一个结构的结构,但如果它存在,我只想通过添加一个新值来更新其中一个键该键的现有值。它不会那样做,我不知道为什么。
代码如下:
<cfloop list="#recipeItemList#" index="ril">
<cfset thisrecipe = entityLoadByPK("recipes", #ril#)>
<cfloop array="#thisrecipe.getrecipeitem()#" index="recipeItems">
<cfif recipeItems.hasrawMaterial_id()>
<cfset theitem = entityLoadByPK("rawMaterials", recipeItems.getrawMaterial_id().getid())>
<cfinvoke method="getPrepByDate" component="/FEcomponents.production" returnvariable="makeAmount">
<cfinvokeargument name="recipe_id" value="#ril#">
<cfinvokeargument name="delivery_date" value="#delivery_date#">
</cfinvoke>
<cfset thisRecipeYield = #thisRecipe.getYield()#>
<cfif thisRecipeYield neq 0>
<cfset recipeRatio = makeAmount / thisRecipeYield>
</cfif>
<cfset thisQuantity = recipeRatio * #recipeItems.getitemQuantity()#>
<cfif structKeyExists(outerstruct, '#theitem.getid()#')>
<!--- we must add this quantity to the existing key, not create a new one --->
<cfset addthis = #outerstruct[theitem.getid()].totalToGet# + #thisQuantity#>
<cfset structInsert(outerstruct[theitem.getid()],'totalToGet', #addthis#,true)>
<!--- this is just to for testing to see if the value has changed, and it does not the 'addthis' value is always the latest value in the loop--->
#outerstruct[theitem.getid()].totalToGet# + #thisquantity# = #addthis# <BR>
<cfelse>
<!--- create a new struct --->
<cfset outerstruct[theitem.getid()] = structnew()>
<cfset outerstruct[theitem.getid()].rawMaterialid = #theitem.getid()#>
<cfset outerstruct[theitem.getid()].rawMaterialName = #theItem.getName()#>
<cfset outerstruct[theitem.getid()].totalToGet = #thisQuantity#>
<cfset outerstruct[theitem.getid()].uom = #theItem.getUOM().getname()#>
</cfif>
</cfif>
</cfloop>
</cfloop>
我哪里做错了。
感谢您的帮助
我认为这会满足您的要求,我删除了一些额外的井号并更改了您更新结构键的方式。抱歉,我无法 运行 代码,但我相信这应该可行。
<cfloop list="#recipeItemList#" index="ril">
<cfset thisrecipe = entityLoadByPK("recipes", #ril#)>
<cfloop array="#thisrecipe.getrecipeitem()#" index="recipeItems">
<cfif recipeItems.hasrawMaterial_id()>
<cfset theitem = entityLoadByPK("rawMaterials", recipeItems.getrawMaterial_id().getid())>
<cfinvoke method="getPrepByDate" component="/FEcomponents.production" returnvariable="makeAmount">
<cfinvokeargument name="recipe_id" value="#ril#">
<cfinvokeargument name="delivery_date" value="#delivery_date#">
</cfinvoke>
<cfset thisRecipeYield = thisRecipe.getYield()>
<cfset thisQuantity = recipeItems.getitemQuantity()>
<cfif thisRecipeYield neq 0>
<cfset recipeRatio = makeAmount / thisRecipeYield>
<cfset thisQuantity *= recipeRatio>
</cfif>
<cfif structKeyExists(outerstruct, theitem.getid())>
<!--- we must add this quantity to the existing key, not create a new one --->
<cfset outerstruct[theitem.getid()].totalToGet += thisQuantity>
<cfelse>
<!--- create a new struct --->
<cfset outerstruct[theitem.getid()] = structnew()>
<cfset outerstruct[theitem.getid()].rawMaterialid = theitem.getid()>
<cfset outerstruct[theitem.getid()].rawMaterialName = theItem.getName()>
<cfset outerstruct[theitem.getid()].totalToGet = thisQuantity>
<cfset outerstruct[theitem.getid()].uom = theItem.getUOM().getname()>
</cfif>
</cfif>
</cfloop>