Solved

Coldfusion loop through a list of pairs name  -  value

Posted on 2016-10-19
3
36 Views
Last Modified: 2016-10-19
Hello experts.
I'm using jquery serialize function in a form in a pop up window to store temporaly the values of name and value of a form in a hidden field (myhiddenfield) in my main form.
so my hidden field has a value like name_1=value_1&name_2=value_2&name_3=value_3
Submitting the form I'm taking the myhiddenfield value as a list with delimiter the & sign
<cfloop list="#myhiddenfield#" index="item" delimiters="&">
  <cfset firstcolumn =  listfirst(item,'=')>
  <cfset secondcolumn =  listlast(item,'=')>
</cfloop>

The problem i have is if there is not any value for a one key.
f.e.  name_1=&name_2=value_2&name_3=value_3
I get for the first list item the same value for firstcolumn and secondcolumn
For testing i tried the listgetat function like listGetAt(item,2,'=') and i get an error Invalid list index 2 because the listitem has one element.
I have a solution using the listlen function like:
<cfif listlen(item,'=') is 2>
    <cfset secondcolumn = listlast(item,'=')>
<cfelse>
    <cfset secondcolumn = ''>
</cfif>
but i need your opinion if there is another way to handle this.
Thank you in advance
0
Comment
Question by:Panos
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 41850408
Despite the weird name, I like using getToken(). It doesn't throw an error if you supply an index that's greater than the number of elements in the list. It just returns an empty string.

      <cfset firstColumn = getToken(item, 1, "=")>
      <cfset secondColumn = getToken(item, 2, "=")>

You could also convert it to an array, setting includeEmptyFields=true, but it's a bit more wordy:

         <cfset data = ListToArray(item, "=", true)>
         <cfif arraylen(data) gte 2>
          <cfset firstColumn = data [1]
          <cfset secondColumn = data[2]>
         </cfif>
0
 
LVL 2

Author Comment

by:Panos
ID: 41850421
Thank you again for your help
0
 
LVL 52

Expert Comment

by:_agx_
ID: 41850475
Welcome :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now