5 Replies Latest reply on Feb 24, 2008 12:23 PM by -==cfSearching==-

    Array question

    DCwebGuy Level 1
      I am trying to pass the following name/value lists to a component:

      attributeNameList: fname,lname,org,city,state,zip,email
      attributeValueList: Joe,Blow,,,,32828,

      I need to assemble these lists so the output looks like this:

      Attribute.1.Name=fname&Attribute.1.Value=Joe&Attribute.2.Name=lname&Attribute.2.Value=Blow &Attribute.6.Name=zip&Attribute.6.Value=32828

      I thought I could simply loop through ListGetAt(attributeValueList) for each name, but I get an error when it hits the blank ones, and I don't know any other way to associate each name with each value between the two lists.

      Now I'm thinking I need to solve this with an array but I have spent hours trying to figure it out with no luck. Anyone know how? I don't care HOW I achieve the output result, so all suggestions are welcome. Thanks!

      P.S. I'm attaching the desired output string as "code" below because this forum cuts off the text above (for me anyway).
        • 1. Re: Array question
          Dan Bracuk Level 5
          Where are the lists coming from?
          • 2. Re: Array question
            -==cfSearching==- Level 4
            IIRC, at least one of the CF8 list functions has a new attribute that can force CF to recognize empty elements.

            If MX7 or prior, one option is replace the blanks with a space character or some series of character(s) representing a blank. That will force CF to recognize them as list elements. Just remember to check the first and last list elements too.

            attributeValueList: Joe,Blow,(empty),(empty),(empty),32828,(empty)

            Once you have a valid list or array, you can use a loop to construct your new string using listAppend(finalList, ...., "&")
            • 3. Re: Array question
              Level 7
              > Now I'm thinking I need to solve this with an array but I have spent hours
              > trying to figure it out with no luck.

              What have you tried and why doesn't it work?

              • 4. Re: Array question
                DCwebGuy Level 1
                Okay, I figured it out on my own finally. I stumbled upon the ListToArray function, and did this:

                <cfset myNameArray = ListToArray(attributeNameList, ",", "true")>
                <cfset myValueArray = ListToArray(attributeValueList, ",", "true")>

                This function allows you to count the blank spaces as array members, which is exactly what I needed.

                Then it was just a matter of looping over both arrays and putting the values where I needed them, like this:

                <cfset index=1/>
                <cfloop from="1" to="#ArrayLen(myNameArray)#" index="j">
                <cfif #myValueArray[j]# eq ""><cfelse>
                <cfset attributeNameValuePairs = ListAppend(attributeNameValuePairs, "Attribute.#index#.Name=#myNameArray[j]#Attribute.#index#.Value=#myValueArray[j]#")/>
                <cfset index = index + 1/>

                This loop says that if the value array is blank, skip it, otherwise match up the name/value pairs. When I output attributeNameValuePairs I get the concatenated string exactly the way I wanted. I love ColdFusion.

                • 5. Re: Array question
                  -==cfSearching==- Level 4
                  See what happens when you read the documentation ;-)