Hi Folks,
I could use a hand parsing a textfile db into a CF object.
Here's an example textfile where the first line is the fieldnames (tab char
separates fields):
id RegionID RegionIndexPath RegionIndexPagename
47 23 Facilities/California/LasAngeles/ LAProper.html
3 88 Facilities/Florida/Miami/ DistrictOfMiami.html
I want to loop through each line to populate this CF structure:
<cfset RegionData = StructNew()>
<cfset RegionData[RegionID] = StructNew()>
<cfset RegionData[#RegionID#]["RegionIndexPath"] = #RegionIndexPath#>
<cfset RegionData[#RegionID#]["RegionIndexPagename"] = #RegionIndexPagename#>
Note that I am intentionally 1) ignoring the id field and 2) picking up the
primary key from the second field and 3) all remaining fieldnames become
secondary keys, so for example a textfile which begins with:
id cfidentifier color width height length
should create a CF datastructure like this:
DataStructureName[#cfidentifier#]["color"] = color_fieldvalue
DataStructureName[#cfidentifier#]["width"] = width_fieldvalue
DataStructureName[#cfidentifier#]["height"] = height_fieldvalue
DataStructureName[#cfidentifier#]["length"] = length_fieldvalue
In perl, I could read through the file one line at a time and split the
fieldvalues directly into a datastructure using the split function. I just
don't know how to do something similar in CF. Any pointers, tips, suggestions
are greately appreciated.
elDonrico - 31 Jul 2006 17:06 GMT
not really sure how to do something like this, but, i did something very
similar last week with a clock file that was a .csv
<cfset CRLF = Chr(13) & Chr(10)><!--- Linefeed & carriage return --->
<cffile action="READ" file="#ExpandPath('/uploadFolder')#/clockfileName.csv"
variable="csvData">
<cfloop index="csvRecord" list="#csvData#" delimiters="#CRLF#">
<cfset csvArray = ArrayNew(1)>
<cfset csvCellCount = 1>
<cfloop index="csvCell" list="#csvRecord#" delimiters=",">
<cfset csvArray[variables.csvCellCount] = csvCell>
<cfset csvCellCount = csvCellCount + 1>
</cfloop>
I then uploaded it into an informix db so that i had some structure that was
"callable" with more ease. problem is you have to know the order of the data
youre getting like:
color,width,height,length
cause you would structure your ... structure the same way cause when you
insert it it will look like this...
<cfquery>
INSERT INTO myTable
(color,width,height,length)
VALUES
(
#csvArray[1]#
,
#csvArray[1]#
,
#csvArray[1]#
,
#csvArray[1]#
)
</cfquery>
</cfloop>
hth
elDonrico - 31 Jul 2006 17:08 GMT
not really sure how to do something like this, but, i did something very
similar last week with a clock file that was a .csv
<cfset CRLF = Chr(13) & Chr(10)><!--- Linefeed & carriage return --->
<cffile action="READ" file="#ExpandPath('/uploadFolder')#/clockfileName.csv"
variable="csvData">
<cfloop index="csvRecord" list="#csvData#" delimiters="#CRLF#">
<cfset csvArray = ArrayNew(1)>
<cfset csvCellCount = 1>
<cfloop index="csvCell" list="#csvRecord#" delimiters=",">
<cfset csvArray[variables.csvCellCount] = csvCell>
<cfset csvCellCount = csvCellCount + 1>
</cfloop>
I then uploaded it into an informix db so that i had some structure that was
"callable" with more ease. problem is you have to know the order of the data
youre getting like:
color,width,height,length
cause you would structure your ... structure the same way cause when you
insert it it will look like this...
<cfquery>
INSERT INTO myTable
(color,width,height,length)
VALUES
(
#csvArray[1]#
,
#csvArray[1]#
,
#csvArray[1]#
,
#csvArray[1]#
)
</cfquery>
</cfloop>
hth
PerlProgrammer - 31 Jul 2006 17:17 GMT
Thanks elDonrico. You've shown me the key, which was how to break the file into records (single lines) then each line into fields (individual records).
PerlProgrammer - 31 Jul 2006 18:20 GMT
Here's what I came up with. Hopefully it'll be useful for someone else too :)
<cffile action="READ" file="#publisher_datadir#RegionData.txt"
variable="RegionData_Textfile">
<cfset RecordDelimiter = Chr(13) & Chr(10)><!--- Linefeed & carriage return
--->
<cfset FieldDelimiter = Chr(9)> <!--- tab --->
<cfset RegionData = StructNew()>
<cfset RegionData_Fieldnames = ArrayNew(1)>
<cfset RegionData_Fieldvalues = ArrayNew(1)>
<cfloop index="SingleRecord" list="#RegionData_Textfile#"
delimiters="#RecordDelimiter#">
<cfif ArrayIsEmpty(RegionData_Fieldnames)> <!--- determine fieldnames from
line 1 --->
<cfset RegionData_Fieldnames = ListToArray(#SingleRecord#,
"#FieldDelimiter#")>
<cfelse> <!--- parse records into datastructure --->
<cfset RegionData_Fieldvalues = ListToArray(#SingleRecord#,
"#FieldDelimiter#")>
<cfset RegionData[#RegionData_Fieldvalues[2]#] = StructNew()> <!--- note:
using second field as primary key, not the textdb's own id field --->
<cfloop index="fieldname_idx" from="3"
to="#ArrayLen(RegionData_Fieldnames)#">
<cfset
RegionData[#RegionData_Fieldvalues[2]#]["#RegionData_Fieldnames[fieldname_idx]#"
] = #RegionData_Fieldvalues[fieldname_idx]#>
</cfloop>
</cfif>
</cfloop>
elDonrico - 31 Jul 2006 18:44 GMT
Great! i'm glad you could get it working... thanks for posting the code so it can help others. if you check it answered it would help even more.