Link to home
Start Free TrialLog in
Avatar of jfreeman2010
jfreeman2010Flag for United States of America

asked on

coldfusion struct insert item and output

coldfusion struct help


1) I am creating a struct, when doing 'structInsert', getting a error:  this key already exists.  how to use structkeyexist around the structInsert?
2) the struct can have item from 1 - 6, struct output will look like this:
   Key            Value
   V72.61            1
   V72.62            2
   V72.63            3
   V72.64            4
   V72.65            5
   V72.66            6
   
   I need to output the key and value, if items in struct more then 4, need to output to the next line, how can I make a loop count what will stop output struct at 4 (output first line), and continue to output the next 2 struct items in the next line?

Thank you very much for help.
Avatar of gdemaria
gdemaria
Flag of United States of America image

<cfif NOT structKeyExits(myStruct,theKey)>
      <!--- does not exist, so go ahead and add new key --->
     <cfset myStruct[theKey] = theValue>
</cfif>

...no need for structInsert, just assign it like a variable as shown, but either way you'd like to do it.  The question was about the CFIF statement..
Avatar of jfreeman2010

ASKER

Hi gdemaria,

Thank you very much for help. Yes, what works for get rid of the duplicate.

Do you know how can I output the struct key and value only output the first 4 items in the struct ( I am had 5 items in the struct now) and output the rest next?

Thank you very much!!!
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
why the output is NOT in order?

1) The value of V72.66 is 5
2) The value of V72.61 is 1
3) The value of V72.62 is 2
4) The value of V72.63 is 3
 
 Thanks
the struct dump look like this:

struct
V72.61 1  
V72.62 2  
V72.63 3  
V72.65 4  
V72.66 5
I am very close, the code look like this now:

<cfset count = 0>
<cfloop index="aKey" list="#structKeyList(theStruct)#">  
    <cfset count = count + 1>
    <cfif count LE 4>
         #count#) The value of #aKey# is #theStruct[aKey]# Line1<br>
    <cfelse>
         #count#) The value of #aKey# is #theStruct[aKey]# Line2<br>
    </cfif>
</cfloop>

Only need the struct output sort by the value.
Structures have no order, arrays are ordered by their numeric element number, but structures are direct access storage.   So, you want to order by the key (easier) or the value (harder)?     You have to setup a mechanism to sort by either one...
I need the output key order by the value as:

V72.61 1  
V72.62 2  
V72.63 3  
V72.65 4  
V72.66 5
 
So value=1, key (V72.61) should output first, value=2, key (V72.62) should output second.

Thank you,
Coldfusion has a function stuctSort that will sort your structure by value.  It will return an array of the keys in the correct order.  So, instead of using your CFLOOP over the list of keys as I showed, you would just loop over the array that structSort returns.

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7efc.html
yes, that works.  thank you very much for your help
thanks