[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Coldfusion loop through a list of pairs name  -  value

Posted on 2016-10-19
3
Medium Priority
?
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

649 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