Solved

Converting a string into a array of structures

Posted on 2014-11-29
4
74 Views
Last Modified: 2016-03-22
I have a string which looks like this

|IFX,I11192014012,1432310019,SW80,CONFIRMED,2014-11-26 15:49:45|IFX,I11192014006,1432310013,SW80,CONFIRMED,2014-11-26 12:27:28

| = beginning of a new Record and fields are separated via , what is the easiest way  convert this into multiple structures inside an array ?
0
Comment
Question by:AlexPonnath
[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
4 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 250 total points
ID: 40472230
looked like a fun challenge, here is some code.  There are two loops, one loops records, one loops the fields.  
In the first line, enter your field names, make sure they are valid variable names.


<cfset fieldNames = "A,B,C,D,E,F"> <!--- put your field names for the structure here ---->

<cfset dataSet = arrayNew(1)>
<cfset theString = "|IFX,I11192014012,1432310019,SW80,CONFIRMED,2014-11-26 15:49:45|IFX,I11192014006,1432310013,SW80,CONFIRMED,2014-11-26 12:27:28">
<cfloop index="aRow" list="#theString#" delimiters="|">
   <cfset pos = arraylen(dataSet) + 1>
   <cfset dataSet[pos] = structNew()>
   <cfset fieldNo = 0>
   <cfloop index="aVal" list="#aRow#" delimiters=",">
      <cfset fieldNo = fieldNo + 1>
      <cfset thisField = listGetAt(fieldNames,fieldNo)> <!--- get the name of this field for the structure --->
      <cfset dataSet[pos][thisField] = aVal> <!---- put the value into this structure for the record --->
   </cfloop>
</cfloop>

<cfdump var="#dataSet#">

Open in new window

0
 
LVL 29

Assisted Solution

by:Pravin Asar
Pravin Asar earned 250 total points
ID: 40480032
May be this will work for you.

These few lines of codes convert the string into 2D Array. Looks like

<!--- Data String --->
<cfset mydata="|IFX,I11192014012,1432310019,SW80,CONFIRMED,2014-11-26 15:49:45|IFX,I11192014006,1432310013,SW80,CONFIRMED,2014-11-26 12:27:28"/>
<!--- Declare 2D Array --->
<cfset recordArray = ArrayNew(2)/>
<cfset ix =1/>
<cfloop list="#myData#" index="curData" delimiters="|" >
<cfset recordArray[ix]=ListToArray(curData,",")/>
<cfset ix++/>
</cfloop>
<cfdump var="#recordArray#"/>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 41518287
Yes, sorry I missed this question :)

An array of structures is more intuitive, but a 2d array is simpler.   Whichever option you use - do not forget to handle empty elements, if that's a possibility, because CF discards them by default.

If you really do need an array of structures,  go with gd's approach, but try arrays rather than lists which simplifies things a little.

<!--- result column names --->
<cfset columnNames = listToArray("A,B,C,D,E,F")>

<!--- sample input string --->
<cfsavecontent variable="inputValue">
|IFX,I11192014012,1432310019,SW80,CONFIRMED,2014-11-26 15:49:45|IFX,I11192014006,1432310013,SW80,CONFIRMED,2014-11-26 12:27:28"
</cfsavecontent>

<cfset results = []>
<cfloop index="line" list="#trim(inputValue)#" delimiters="|">
   <!--- extract current values into array, PRESERVE empty values --->
   <cfset columnValues = listToArray(line, ",", true)>
   
   <!--- build a new structure and populate with current values --->	
   <cfset rowData = {}>
   <cfloop from="1" to="#arrayLen(columnNames)#" index="x">
      <cfset rowData[ columnNames[x] ] = columnValues[ x ]>
   </cfloop>
   
   <!--- save the current row --->
   <cfset arrayAppend( results, rowData )>
</cfloop>

<cfdump var="#results#">

Open in new window

0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 41518460
Thanks for trying to improvise and get the better solution.
0

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

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…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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