Solved

Coldfusion loop through a list of pairs name  -  value

Posted on 2016-10-19
3
70 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 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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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