views:

1434

answers:

2

I have an 2 dimensional array. I write 3 values into the array during its initialization and i add a fourth value if an array value is not equal with a value passed through a form. Then i want to check, if the fourth value exists.

update.cfm

<cfset array = obj.getArray() />
<cfif not StructIsEmpty(form)>
  <cfloop collection="#form#" item="key">
    <cfif left(key,3) eq "ID_">
      <cfset number = listLast(key,"_") />
      <cfset value = evaluate(0,key) />
      <cfloop index="j" from="1" to="#arrayLen(array)#">
        <cfif (array[j][1] eq number) and (array[j][3] neq value)>
          <cfset array[j][3] = value />
          <cfset array[j][4] = "true" />
        </cfif>
      </cfloop>
    </cfif>
  </cfloop>
<cfset obj = createObject("component", "cfc.Obj").init(arg = form.arg, argarray = array) />
<cfset application.objDao.update(obj) />

objDao.cfc update method

<cfset matarray = arguments.obj.getArray() />
  <cfloop index="i" from="1" to="#arrayLen(array)#">
    <cfquery name="qUpdateAsset" datasource="#variables.instance.dsn#">
      UPDATE table
      SET value = <cfqueryparam value="#matarray[i][3]#" cfsqltype="cf_sql_integer" />

    <!--- This is wrong, how do i check the existence correctly? --->
    <cfif StructKeyExists(matarray[i],"4")>
      ,status = 'true'
    </cfif>

      WHERE arg = <cfqueryparam value="#arguments.obj.getArg()#" cfsqltype="cf_sql_smallint" />
        AND number = <cfqueryparam value="#matarray[i][1]#" cfsqltype="cf_sql_integer" />
  </cfquery>
</cfloop>

My wrong attempt results in this error:

You have attempted to dereference a scalar variable of type class coldfusion.runtime.Array as a structure with members.

A: 

Well for starters, I'm not a huge Cold Fusion guy... but I'm pretty sure that you can't use StructKeyExists on an Array, since its not a struct.

Also, have you tried something like

<cfset testValue = matarray[i][4]>
<cfif isDefined testValue>
    ...
</cfif>
Boushley
you are of course right, stupid me
mrt181
+6  A: 

I believe you just need to check the array length like so:

<cfif ArrayLen(matarray[i]) gte 4>
      ,status = 'true'
</cfif>
Soldarnal
that did the trick
mrt181