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.
jfreeman2010Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gdemariaCommented:
<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..
0
jfreeman2010Author Commented:
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!!!
0
gdemariaCommented:
<cfset count = 0>
<cfloop index="aKey" list="#structKeyList(theStruct)#">  <!---- loop a list of the keys ---->
     <cfset count = count + 1>
      #count#) The value of #aKey# is #theStruct[aKey]#<br>
    <cfif count gte 4> <!--- break the loop after 4 ---->
      <cfbreak>
   </cfif>
</cfloop>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

jfreeman2010Author Commented:
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
0
jfreeman2010Author Commented:
the struct dump look like this:

struct
V72.61 1  
V72.62 2  
V72.63 3  
V72.65 4  
V72.66 5
0
jfreeman2010Author Commented:
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.
0
gdemariaCommented:
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...
0
jfreeman2010Author Commented:
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,
0
gdemariaCommented:
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
0
jfreeman2010Author Commented:
yes, that works.  thank you very much for your help
0
jfreeman2010Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.