Solved

Converting a string into a array of structures

Posted on 2014-11-29
4
58 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
  • 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 28

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 28

Expert Comment

by:Pravin Asar
ID: 41518460
Thanks for trying to improvise and get the better solution.
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

Suggested Solutions

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A short film showing how OnPage and Connectwise integration works.

914 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

16 Experts available now in Live!

Get 1:1 Help Now