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

Array question

Contributor ,
Feb 23, 2008 Feb 23, 2008

Copy link to clipboard

Copied

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).
TOPICS
Advanced techniques

Views

391

Translate

Translate

Report

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

correct answers 1 Correct answer

Contributor , Feb 24, 2008 Feb 24, 2008
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(myName...

Votes

Translate

Translate
LEGEND ,
Feb 23, 2008 Feb 23, 2008

Copy link to clipboard

Copied

Where are the lists coming from?

Votes

Translate

Translate

Report

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 ,
Feb 23, 2008 Feb 23, 2008

Copy link to clipboard

Copied

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, ...., "&")

Votes

Translate

Translate

Report

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
LEGEND ,
Feb 23, 2008 Feb 23, 2008

Copy link to clipboard

Copied

> 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?

--
Adam

Votes

Translate

Translate

Report

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
Contributor ,
Feb 24, 2008 Feb 24, 2008

Copy link to clipboard

Copied

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# eq ""><cfelse>
<cfset attributeNameValuePairs = ListAppend(attributeNameValuePairs, "Attribute.#index#.Name=#myNameArray#Attribute.#index#.Value=#myValueArray#")/>
</cfif>
<cfset index = index + 1/>
</cfloop>

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.

Votes

Translate

Translate

Report

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 ,
Feb 24, 2008 Feb 24, 2008

Copy link to clipboard

Copied

LATEST
See what happens when you read the documentation 😉

Votes

Translate

Translate

Report

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
Documentation