Solved

Converting a string into a array of structures

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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 …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

762 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