Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Inserting Structures into an Array

Engaged ,
Jun 11, 2008 Jun 11, 2008
Hi,

I was looking through the Internet for solutions on how to insert structures into an array. I have worked this out and all is fine. However, although my code works, can you see any problems with the way I have done it?

I basically create an array and then append structures into it. However, I needed my structures to have the same name. So, I keep creating a new structure with that name (cfc_info) and assign to it two parts (type and message).

Then, I can loop the array and access all the structures in the same way, without having to know the name of the actual structures themselves.

Let me just stress this...it does actually do what I want...but I'm wondering if there could be problems with creating structures that have the same name this way? I have not had any errors etc but it seems very odd that I can do it this way.

If anyone has a better suggestion and explanations etc it'd be much appreciated.

Thanks,
Mikey.
407
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 11, 2008 Jun 11, 2008
Forgot to attach the code:

<cfset cfc_array = arrayNew(1) />

<cfset cfc_info = structNew() />
<cfset cfc_info.type = "error" />
<cfset cfc_info.message = "User Name and Password are both required." />
<cfset ArrayAppend(cfc_array,cfc_info) />

<cfset cfc_info = structNew() />
<cfset cfc_info.type = "error" />
<cfset cfc_info.message = "dgdfgdgd dgdg dg dfg " />
<cfset ArrayAppend(cfc_array,cfc_info) />

<cfset cfc_info = structNew() />
<cfset cfc_info.type = "error" />
<cfset cfc_info.message = "Another structure test" />
<cfset ArrayAppend(cfc_array,cfc_info) />

<cfif isDefined("cfc_array")>

<cfdump var="#cfc_array#" />

<cfloop index="message" array="#cfc_array#">
<cfoutput>TYPE = #message.type# MESSAGE = #message.message#<br /></cfoutput>
</cfloop>

</cfif>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 11, 2008 Jun 11, 2008
As long as you remember exactly where in the array each structure is, it shouldn't be a problem. But when it gets to be quite large, remembering the one you need at postion 56 say, might be an issue.

I'd put a 'name' field in the cfc_info structure, so you can name each structure and access them like that when looping through if you need a specific one. Also, each structure will have a unique 'name' even though they are all called the same. If that makes sense.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 11, 2008 Jun 11, 2008
I wont need to remember the position of the structure items because the idea here is that I'll be looping them for messages that arrive from a CFC. Because of this, I create them sharing the same names. As long as there's no problem here that's great. Like I said it works...but I was just a bit concerned - I'm not a professional programmer or anything so I'm always wary of what I'm doing.

Thanks!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 13, 2008 Jun 13, 2008
LATEST
> I'll be looping them for messages that arrive from a CFC

Since you mentioned a CFC is involved, the one suggestion I would make is to ensure the variables are properly scoped. Proper scoping will avoid any unintended scope conflicts that might crop up.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources