Solved

Converting a string into a array of structures

Posted on 2014-11-29
4
54 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
cfselect bind Not return selected value in dropdown 4 73
multi page pdf 4 69
paging 3 28
ColdFusion 10 Error 2 22
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 …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

18 Experts available now in Live!

Get 1:1 Help Now