Curious13
Curious13

Reputation: 329

loop through implicit array

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>

Upvotes: 3

Views: 203

Answers (2)

Shawn
Shawn

Reputation: 4786

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.

Upvotes: 1

Curious13
Curious13

Reputation: 329

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>

Upvotes: 3

Related Questions