Solved

Converting a string into a array of structures

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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