Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Coldfusion loop through a list of pairs name  -  value

Posted on 2016-10-19
3
Medium Priority
?
97 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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 …
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

810 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