3

I've been stuck on this for awhile now. I'm trying to loop through this array so I can perform some calculations but I cannot figure out how to loop through there values. Any suggestions?

I managed to figure out how to get there collection structures but I want to loop through each structure and grab there values as well and thats what I'm stuck on.

Also, I want to refrain from using cfscript if possible as I'm still in the learning stages of learning coldfusion.

Here is my code:

<cfset houseStuff = {
    Bedroom = [
        'Luxury Duvet Set with Alternative Down Comforter',
        'Accent Coverlet & Shams',
        'Two Sets of Luxurious Liens',
        'Mattress Pad',
        'Blanket',
        'Six Bed Pillows',
        'Clock Radio',
        'Twenty Hangers'
    ],
    Bathroom = [
        'Four Bath Towels',
        'Four Hand Towels',
        'Four Face Towels',
        'Bath Rug',
        'Shower Curtain',
        'Stainless Tooth Brush Holder & Soap Dish',
        'Wastebasket',
        'Artwork',
        'Hair Dryer',
        'Toilet Brush & Plunger'
    ],
    Dining = [
        'Dinnerware',
        'Place Mats',
        'Napkins',
        'Flatware',
        'Glassware & Wine Glasses'
    ],
    Kitchen = [
        'Microwave',
        'Cookware',
        'Mixing Bowls',
        'Baking Dish',
        'Colander',
        'Stainless Utensil Holder',
        'Large Fork',
        'Large Spoon',
        'Spatula',
        'Whisk',
        'Measuring Spoon & Cup',
        'Carving & Paring Knives',
        'Four Steak Knives',
        'Cutting Board',
        'Salt & Pepper Set',
        'Wine Opener',
        'Coffee Maker',
        'Toaster',
        'Electric Can Opener',
        'Flatware Tray',
        'Kitchen Wastebasket',
        'Dish Towels',
        'Pot Holders',
        'Pitcher',
        '10" Non-Stick Frying Pan',
        'Cookie Sheet',
        'Stainless Steel Electric Tea Kettle',
        '3 Piece Non-Metal (Spatula, Spoon, Paste Spoon) Combo'
    ],
    Micellaneous = [
        'Iron & Cutting Board',
        'Cordless Dual Phone with Digital Answering Machine',
        'Broom',
        'Dust Pan',
        'Vacuum',
        'Decor',
        'Laundry Basket'
    ],
    StarterKit = [
        'Bath Tissue',
        'Soap',
        'Shampoo & Conditioner',
        'Paper Towels',
        'Sponge',
        'Laundry Soap',
        'Dishwasher Detergent',
        'Liquid Dish Soap',
        'Extra Light Bulbs',
        'Coffee',
        'Sugar',
        'Creamer',
        'Bottled Water',
        'Oatmeal',
        'Breakfast Bars',
        'Peanuts',
        'Chips',
        'Mints',
        'Welcome Information'
    ],
    MasterBedroom = [
        'Queen bed',
        'Headboard',
        'Two Nightstands',
        'Dresser & Mirrior',
        'Two Lamps',
        'Artwork',
        'LCD Television'
    ],
    LivingRoom = [
        'Sofa',
        'Chair',
        'End Table',
        'Coffee Table',
        'Lamp',
        'LCD TV w/stand',
        'DVD Player',
        'Artwork'
    ],
    DiningRoom = [
        'Dining Table',
        'Dining Chairs',
        'Artwork'
    ],
    OfficePackage = [
        'Desk',
        'Chair',
        'Lamp'
    ],
    AdditionalBedrooms = [
        'Queen or Two Twin Beds',
        'Headboard',
        'Nightstand',
        'Chest of Drawers',
        'Lamp',
        'Artwork'
    ]
} />

<cfloop collection="#houseStuff#" item="key">
    <cfdump var="#key#"> <br>
    <!--- <p style="color:##fff;">#key#:</p> <br /> --->
</cfloop>
3
  • What version of ColdFusion? If you're learning, I'd stick with the newer versions. There's a good bit of difference between CF9 or less and the versions above CF10. And a fair bit of difference between CF10 and CF2016. Commented Dec 6, 2017 at 15:43
  • Dumped out the Server variable. I'm using 9. Commented Dec 6, 2017 at 17:21
  • Is CF9 for work or for learning? If for work, then your kinda stuck, but pay attention to the version you learn. You'll see a lot of learning info for CF10+ (and I think learncfinaweek.com is CF10), but there were quite a few changes between 9 and 10. Many things got easier to do after 10. If it's just for learning, I'd bump up to a higher version. Commented Dec 6, 2017 at 20:41

2 Answers 2

3

Nevermind, I finally figured it out. I had to loop through the collection first. Once I do that create another loop inside it to loop over it's structured values.

<cfloop collection="houseStuff" item="key">
   <!---<cfdump var="#houseStuff[key]"> --->
    <cfloop from="1" to="#arrayLen(houseStuff[key])#" index="j">
        #j#
    </cfloop>
</cfloop>
Sign up to request clarification or add additional context in comments.

15 Comments

Unless you specifically need the position, an "array" loop is a little simpler: <cfloop array="#arr#" index="elem"><cfdump var="#elem#></cfloop>
My vote put your reputation into triple digits. You owe me a beer.
@Ageax Note: In this situation, the array loop won't work. This is a structure of arrays, not an array of arrays.
I didn't realize that ColdFusion 7 supported the syntax in your question. But it does not support array loops.
cfloop array="" was added in CF8, but I think it was wonky until 9.0.1. Can't remember. The error may be from using it in the outer loop. The outer loop would stay collection, where the inner loop would be array.
|
1

I know you said you'd prefer tags instead of script, but if you are in the learning stages of ColdFusion, I'd still recommend learning how to properly use cfscript. In addition to making your CF a little bit cleaner, it will also make your life a lot easier, especially for things like looping.

Outputting all elements becomes:

<cfscript>
for ( i in houseStuff ) { // loop over the outer Structure
    writeOutput(i & ":<br>") ;
    for ( j in houseStuff[i] ) { // loop over each inner Array key
        writeOutput(j & "<br>") ;
    }
    writeOutput("<br>");
}
</cfscript>

https://trycf.com/gist/898988f6969a57aa5dece39c42037cfd/acf?theme=monokai

... which, in this context, does get into the philosophical discussion of whether to write output code in tags or script and goes slightly beyond the scope of this question. But I've always been a proponent of learning best-practices at the same time as the basics. Personally, I do tend to follow the tags-for-output view, but for basic looping, the script version is a bit cleaner to me. I'd learn both.

Also check out: http://www.learncfinaweek.com. There's a section in there on Looping with both methods.

2 Comments

Thanks for the suggestion. I appreciate it. cfscript looks very similar to how JavaScript is written and I have the basics of it down so I'll make sure to try to get into cfscript in the future. It's just I'm trying to focus in on what I know until I get more comfortable.
@Curious13 cfscript is very similar to javascript. That's one of the things that makes it nice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.