954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

struct issue

Hi I was following a tutorial on sitepoint for creating structs. I must be missing something since I followed the code exactly, but keep getting an error!

Here is the code:

<cfset myBook= structNew()>
<cfset a = StructureInsert(myBook, \\"title\\", \\"All About Coldfusion\\", 1)>
<cfset a = StructureInsert(myBook, "author", "Teed Younger", 1)>
<cfset a = StructureInsert(myBook, "description", "Information About CF", 1)>
<cfset a = StructureInsert(myBook, "publishYear", "2005", 1)>
<cfset a = StructureInsert(myBook, "ISBN", "ABCD123456", 1)>


<cfoutput>
Title: #myBook["title"]#
Author: #myBook["author"]#
Description: #myBook["description"]#
Published: #myBook["publishYear"]#
ISBN: #myBook["ISBN"]#
</cfoutput>


The error says I have a missing arguement and cites line 10, but I dont see what I am missing. I also dont understand what the backslashes are for in the title struct. When I take them out to match the other structureInserts, it gives another error saying I haven't defined structureInsert. I am new to learning structs so any help is greatly appreciated!

teedoff
Posting Pro
599 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
 
I followed the code exactly

I hope not ... because that code is wrong :) Two things are messing it up
1) Assuming the \\ is not a typo. Get rid of them.
2) The function is StructInsert ... not StructureInsert

But that function isn't used as much as it used to be. Most times it's simpler to just use array notion. That's my preference. But all of these are valid

<cfset StructInsert(myBook, "author", "Teed Younger", true)>
   ... or ....
   <cfset myBook.author = "Teed Younger"> 
   ... or ....
   <cfset myBook["author"] = "Teed Younger">

It's not the cause of the error, but there's no need to capture the result if you're not using it.

<cfset StructInsert(myBook, "author", "Teed Younger", true)>
arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

Btw, where is this tutorial? It sounds outdated.

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

lol I'm so dumb! lol Seriously I do think I'm getting dyslexic!!

Changed the structureInsert(which I could have sworn I saw!!) and now its correct function.

The backslashes are actually there in the tutorial! I swear...lol

Yes it may be outdated and yes I'm sure this way is not the best way to do it, but I'm thinking as the tutorial goes along, they will be using more modern approaches.

Here is the tutorial .

teedoff
Posting Pro
599 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
 

Figures the back slashes are actually in the code. lol. That one must have gotten past the editor.

Yeah, the article is definitely outdated. Most of it still applies. But StructInsert isn't as commonly used now as it was in 2003 Nowadays most people just use array notation. Simpler and more intuitive.

For the lazy, there's even shortcuts in CF8+. So instead of doing

<cfset myBook = structNew()>
<cfset myBook.publishYear = "2005">
<cfset myBook.ISBN= "ABCD123456">


You can do it all in one line

<cfset myBook = {publishYear = "2005", ISBN= "ABCD123456"}>


But it's mostly personal preference. For long structures, the line by line approach is perfectly fine.

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 
1) <cfset myBook.author = "Teed Younger">
... or ....
2) <cfset myBook["author"] = "Teed Younger">


Personally, I prefer syntax 1 (dot notation). Either is okay unless your key name isn't a valid variable name. Like it has spaces or dashes in it ie myBook["some-author"]. Then you must use syntax 2.

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

Well this all started because I was trying to figure out how to display text in a dynamci list. I started another thread here asking about it. On another forum I was told how bad the way I was doing this was....to which I agreed, but since I myself have no experience in jQuery or ajax, I declinded to use the suggestion someone provided. Then they worked up a demo for me and gave me the code. I said it was way over my head but I would love to use it. After two and a half days of trying to understand it, I asked for more help in understanding how to pass the values on the form input fields to my action page. I was told he didnt feel like helping me anymore..lol

Anyway, so since I need to start learning this anyway, I thought I better get to it..lol

ahhh lol shoulda known you were the one helping me in my last post "Another way to do this" so you're familiar with what I'm trying to do.

I have that version of what I was trying to code working fine, but as you know and we discussed, its a nightmare for the person entering the data. Keeping which delimiter goes where, when to hit enter, when to just let a soft return happen.

teedoff
Posting Pro
599 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
 

Yeah, I saw that thread. Being fair that's not what he was saying :P I think the questions were just to broad/ambiguous to be answered. I know some of the background, and even I was intimidated by the sheer scope of it ;-) No joke, I wasn't even sure what the question was .. let alone where to begin trying to answer it.

I think we both love learning and teaching. But there's only so much you can do in a forum post. Forums are good for small, specific questions. Broad comments like "how do I make it work" or "tried everything, and nothing works" aren't questions that can be answered. They just sound like someone is asking others to write the code for them. I know that not's what you were doing. I'm just trying to explain why they responded the way they did. I think if you'd tried the code, and come back with a specific problem (however small), the response would have been better.

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

I hope you didn't take my remarks the wrong way. I just didn't have a clue where to begin with your question on the other forum. As strange as it sounds, it was as overwhelming to me as jquery probably is to you right now ;-)

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

Oh no...no problem. I think your last post confused me somewhat though..lol

My last post was in reference to a thread I started on adobe's forums, where I got some code from someone. Yes I probably did sound too generic when I posted I was lost and needed help understanding the code. I guess I should have been more specific and specified a legitimate question about a certain issue. I actually thought thats what I had done. Even though I didnt understand much of the code he gave me, I thought I could at least figure out how to display the form inputs on my action page. Not so easy....lol I mean most of his code is jQuery written just to dynamically create controls or objects, for lack of knowledgeable working.

But I guess I took his response as him being highminded. I said I wasnt mad, and I'm not, but just felt like if someone tells you your doing something wrong and then offers some code for you to "use", then they ought to be willing to help you further long if you've already said "it's over my head".

I've been working all day off and on trying to learn structs and arrays of structs, and have a somewhat good understanding. Now I just need to figure out how to display the struct his code contains, along with the form input values on my action page.

Anyway, no worries. You've been a great help to me here! As I said before, I wish there were more Coldfusion forums, and more activity on the few that are around.

teedoff
Posting Pro
599 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
 

lol. The confusion is spreading ;-)

But I guess I took his response as him being highminded. I said I wasnt mad, and I'm not, but just felt like if someone tells you your doing something wrong and then offers some code for you to "use", then they ought to be willing to help you further long if you've already said "it's over my head".

Yeah, but you have to articulate "what specifically" you need help with :-) ie "I've tried X code and expected Y results. But instead I'm getting Z results." Otherwise people don't know how to answer you. Or worse it sounds like saying "write the code for me". I admit the guy was definitely blunt. But I don't think he was trying to be a jerk. Otherwise, he wouldn't have bothered putting together a custom example for you in the first place :-)

I'm not a jquery guru. So I don't completely understand his sample either. But if it were me, I'd run the example and post it to an action page. Then dump the results and go from there. If it's just structures and arrays, which I think it is, it might not be so bad and Imight actually be able to help!

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

Hey, I just tried it .. and that's a fantastic example! I'm not smart enough to write it, but it's great. Everything's organized into a structures and arrays. Just loop through it and extract what you need.

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

lol yes its a great application! And thats what I'm trying to do...I just dont know enough about struct and arrays...AND how to display user defined values of structs and arrays yet to get it working.


Still learning though.

teedoff
Posting Pro
599 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
 

It's actually pretty easy. Just deserialize the results.

<cfset results = deserializeJSON(url.data)>

Each "item" is a structure. They "key" is the item name, and the "value" are the details (category, vendors, etc..). So loop through it like in the sitepoint article.

<cfloop collection="#results#" item="itemName">
    <cfset item = results[itemName]>
    <cfdump var="#item#" label="Item Name #itemName#">
</cfloop>


If you look at the dump of each "item", that structure contains 2 keys: "category" and "vendors". Category name is a simple string you can access using #item.category#

Now "Vendors" is another structure. The "key" is a vendor name and the "value" is anarray of products. To extract the values, loop through it the same way

<cfloop ....>
   ... 
   <cfloop collection="#item.vendors#" item="vendorName">
       <cfset productArray = item.vendors[vendorName]>   
       <cfdump var="#productArray #" label="Products for vendor: #vendorName#">
   </cfloop>
  ...
</cfloop>


Then loop through the product array to get each value. For CF8+ it's a simple "array" loop

....
   <cfloop array="#productArray #" index="productName">
       ...do something with #productName#....
   </cfloop>
....


Obviously you have to *do* something with the values. But now that you see how to extract them, the rest is easy.

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

Ok, I think I have it working..lol. One question though. Is there a way to alphabetize the dynamic lists?. As it is now, it seems that if you populate one item then go back and add another item, it puts the second one at the top of the output.


Thanks again for all your help!! I still dont quite understand structs like this, I do have a basic understanding, but I'll keep reading and playing around with them.

teedoff
Posting Pro
599 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
 

ok well I have the code working as I want, and displaying in a list as well.

However, when I pull the code into my application....my pageTemplate.cfm and pull the the cfloops over to MY action page, I get an error saying data is undefined.

I was thinking I needed to pass the FORM variables, but that doesnt seem to work either. I dont know...lol I'm pretty confused at this point.

My whole application is reliant on SESSION and FORM variables, then to throw this into the mix makes it even more confusing.

teedoff
Posting Pro
599 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
 

I've no idea what your final code looks like. So I'm not sure what to suggest :)

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

Well here is the link to the whole application. The template.cfm is where the user enters all her data, then when she clicks submit, it takes her to the action page so she can preview how the page will look before finalizing the .html page and saving to a specified folder on our network.


When I throw a cfdump of #results# before the cfoutput for my loop it says [empty string]

teedoff
Posting Pro
599 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
 

Well think about it :) Your code isn't doing the same thing as the example. The example sends data via ajax and displays the results w/out even leaving the current page. You're doing a form post, which obviously leaves the page. Not to mention sends the data in the FORM, not URL, scope. So obviously the sample code won't work.

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

Well thats what I saying above. I figured I somehow needed to pass the form variables.

I'm about ready to scrap this jQuery code. It has me so confused now I dont even know what my original application is doing..lol.

teedoff
Posting Pro
599 posts since Jul 2010
Reputation Points: 21
Solved Threads: 60
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You