Coldfusion根据txt中的值排序行 [英] Coldfusion Ordering rows according to a value in a txt

查看:344
本文介绍了Coldfusion根据txt中的值排序行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个txt文件,并希望根据每一行中的值对所有行进行排序。



什么是最好的和最快的方式来实现?



下面是我用来编译txt文档的代码:

 <!--- CSV FILE ---> 
< cffile action =readfile =C:/ColdFusion10/cfusion/wwwroot/kelly2/debitorders.csvvariable =csvfile>

< cfoutput>
<!--- LOOP THROUGH CSV FILE --->
< cfloop index =indexlist =#csvfile#delimiters =#chr(10)## chr(13)#>

<!--- SET VALUES --->
< cfset accountholder =#listgetAt('#index#',1)##listgetAt('#index#',2)#>
< cfset accountholderlname =#listgetAt('#index#',2)#>
< cfset accountnumber =#listgetAt('#index#',3)#>
< cfset accounttype =#listgetAt('#index#',4)#>
< cfset bankname =#listgetAt('#index#',5)#>
< cfset branch =#listgetAt('#index#',6)#>
< cfset amount =#listgetAt('#index#',7)#>
< cfset date =#listgetAt('#index#',8)#>

<!--- SET INITIAL --->
< cfset initial =#left(accountholder,1)#>

<!--- SET LAST NAME --->
< cfset lname_final =#replace(#accountholderlname#,,,all)#>

<!--- GET AND SET ACC TYPE --->
< cfif accounttype eqcheck>
< cfset accounttype_final =CH>
< cfelseif accounttype eqsavings>
< cfset accounttype_final =SAV>
< cfelseif accounttype eqcredit>
< cfset accounttype_final =CR>
< cfelse>
< cfset accounttype_final =OTH>
< / cfif>

<!--- SET AMOUNT --->
< cfset amount_final = #round(amount * 100)#>
< cfset amount_final = #NumberFormat(amount_final,0000000)#>

<!--- SET DATE --->
< cfset date_final =#DateFormat(Date,ddyyyymm)#>

<!--- TRIM VALUES --->
< cfset initial =#Left(initial,1)#>
< cfset lname_final =#Left(lname_final,14)#>
< cfset accountnumber =#Left(accountnumber,13)#>
< cfset accounttype_final =#Left(accounttype_final,3)#>
< cfset branch =#Left(branch,9)#>
< cfset amount_final =#Left(amount_final,7)#>
< cfset date_final =#Left(date_final,8)#>

<!--- SET STRING LENGTH FOR EACH --->
< cfset initial =#LJustify(initial,1)#>
< cfset lname_final =#LJustify(lname_final,15)#>
< cfset accountnumber =#LJustify(accountnumber,14)#>
< cfset accounttype_final =#LJustify(accounttype_final,3)#>
< cfset branch =#LJustify(branch,10)#>
< cfset amount_final =#LJustify(amount_final,7)#>
< cfset date_final =#LJustify(date_final,8)#>

<!--- SET TOTAL STRING --->
< cfset total_string =#initial ## lname_final ## accountnumber ## accounttype_final ## branch ## amount_final ## date_final#>






< pre>
#accountholder#< br>
#accountnumber#< br>
#accounttype#< br>
#bankname#< br>
#branch#< br>
#amount#< br>
#date#< br>
#initial#< br>
#lname_final#< br />
#accounttype_final#< br>
#amount_final#< br />
#date_final#< br>
123456789012345678901234567890123456789012345678901234567890< br />
#total_string#< br>
< br />
< br />
< / pre>





<!--- IF FILE FOR BANK EXISTS --->
< cfif FileExists(ExpandPath(#listgetAt('#index#',5)#。txt))

<!--- READ现有文件头 - >
< cffile action =readfile =C:/ColdFusion10/cfusion/wwwroot/kelly2/#bankname#.txtvariable =bankheader>
<!---分割标题以在其上添加新值 - >
< cfset numericvalue = listfirst(bankheader,chr(13))>
< cfset numericvalue = #Right(numericvalue,13)#>

< cfset RecordCountvalue = #Left(numericvalue,3)#>
< cfset RecordCountvalue =#RecordCountvalue#+ 1>
< cfset RecordCountvalue = #NumberFormat(RecordCountvalue,000)#>
< cfset RecordCountvalue = #Left(RecordCountvalue,3)#>
< cfset RecordCountvalue = #RJustify(RecordCountvalue,3)#>

< cfset TotalRecordvalue = #Right(numericvalue,10)#>
< cfset TotalRecordvalue =(#TotalRecordvalue#+#amount#)* 100000>
< cfset TotalRecordvalue = #NumberFormat(TotalRecordvalue,0000000000)#>
< cfset TotalRecordvalue = #Left(TotalRecordvalue,10)#>
< cfset TotalRecordvalue = #RJustify(TotalRecordvalue,10)#>

<!--- SET HEADER FOR FILE --->
< cfset fileheader_bank =#UCase(bankname)#>
< cfset fileheader_bank =#Left(fileheader_bank,15)#>
< cfset fileheader_bank =#LJustify(fileheader_bank,16)#>
< cfset newfile_header =#fileheader_bank ## RecordCountvalue ## TotalRecordvalue#>

< pre>
#numericvalue#< br />
#RecordCountvalue#< br />
#TotalRecordvalue#< br />
#newfile_header#
< / pre>
<!--- APPEND FILE AND ADD UPDATED HEADER --->
< cfset bankheader = listSetAt(bankheader,1,#newfile_header#,#chr(13)#)>
< cffile action =write
fixnewline =no
addnewline =no
file =#getDirectoryFromPath(getTemplatePath())#/#listgetAt #index#',5)#。txt
output =#bankheader#>

<!--- APPEND FILE AND ADD NEW ENTRY --->
< cffile action =append
fixnewline =no
file =C:/ ColdFusion10 / cfusion / wwwroot / kelly2 /#listgetAt('#index#',5) #.txt
output =#total_string#>


<!---如果文件的银行不存在--->
< cfelse>
<!--- SET HEADER FOR FILE --->
< cfset fileheader_bank =#UCase(bankname)#>
< cfset fileheader_bank =#Left(fileheader_bank,15)#>
< cfset fileheader_bank =#LJustify(fileheader_bank,16)#>
< cfset newfile_header =#fileheader_bank#001000#amount_final#>

<!--- CREATE NEW FILE WITH BANK NAME --->
< cffile action =write
fixnewline =no
file =C:/ ColdFusion10 / cfusion / wwwroot / kelly2 /#listgetAt('#index#',5) #.txt
output =#newfile_header#>

<!--- APPEND FILE AND ADD NEW ENTRY --->
< cffile action =append
fixnewline =no
file =C:/ ColdFusion10 / cfusion / wwwroot / kelly2 /#listgetAt('#index#',5) #.txt
output =#total_string#>

< / cfif>
< / cfloop>
< / cfoutput>

我不知道如果我开始使用数组来做这件事,请告诉我。



我不知道是否可以检查,因为我插入每一行插入到正确的地方,我从来没有听说过这个。 / p>

如果需要,我还可以在过程结束时循环遍历txt文件。

解决方案

使用cfhttp读取csv文件。 name属性创建一个查询对象。您可以使用Q的Q来执行您的排序,然后继续。



详细信息在cfhttp的文档中。


I have a txt file and would like to order all the rows according to a value in each row.

What is the best and fastest possible way to achieve this?

Below is the code that I use to compile my txt documents:

<!---CSV FILE--->
<cffile action="read" file="C:/ColdFusion10/cfusion/wwwroot/kelly2/debitorders.csv" variable="csvfile">

<cfoutput>
<!---LOOP THROUGH CSV FILE--->
<cfloop index="index" list="#csvfile#" delimiters="#chr(10)##chr(13)#">

        <!---SET VALUES--->
        <cfset accountholder = "#listgetAt('#index#',1)# #listgetAt('#index#',2)#">
        <cfset accountholderlname = "#listgetAt('#index#',2)#">
        <cfset accountnumber = "#listgetAt('#index#',3)#">
        <cfset accounttype = "#listgetAt('#index#',4)#">
        <cfset bankname = "#listgetAt('#index#',5)#">
        <cfset branch = "#listgetAt('#index#',6)#">
        <cfset amount = "#listgetAt('#index#',7)#">
        <cfset date = "#listgetAt('#index#',8)#">

       <!---SET INITIAL--->
       <cfset initial = "#left(accountholder,1)#">

       <!---SET LAST NAME--->
       <cfset lname_final = "#replace("#accountholderlname#"," ","","all")#">

       <!---GET AND SET ACC TYPE--->
       <cfif accounttype eq "cheque">
            <cfset accounttype_final = "CH">
       <cfelseif accounttype eq "savings">
            <cfset accounttype_final = "SAV">
       <cfelseif accounttype eq "credit">
            <cfset accounttype_final = "CR">
       <cfelse>
            <cfset accounttype_final = "OTH">
       </cfif>

       <!---SET AMOUNT--->
       <cfset amount_final = #round(amount * 100)#>
       <cfset amount_final = #NumberFormat(amount_final,"0000000")#>

       <!---SET DATE--->
       <cfset date_final = "#DateFormat(Date,"ddyyyymm")#">

       <!---TRIM VALUES--->
       <cfset initial = "#Left(initial, 1)#">
       <cfset lname_final = "#Left(lname_final, 14)#">
       <cfset accountnumber = "#Left(accountnumber, 13)#">
       <cfset accounttype_final = "#Left(accounttype_final, 3)#">
       <cfset branch = "#Left(branch, 9)#">
       <cfset amount_final = "#Left(amount_final, 7)#">
       <cfset date_final = "#Left(date_final, 8)#">

       <!---SET STRING LENGTH FOR EACH--->
       <cfset initial = "#LJustify(initial, 1)#">
       <cfset lname_final = "#LJustify(lname_final, 15)#">
       <cfset accountnumber = "#LJustify(accountnumber, 14)#">
       <cfset accounttype_final = "#LJustify(accounttype_final, 3)#">
       <cfset branch = "#LJustify(branch, 10)#">
       <cfset amount_final = "#LJustify(amount_final, 7)#">
       <cfset date_final = "#LJustify(date_final, 8)#">

       <!---SET TOTAL STRING--->
       <cfset total_string = "#initial##lname_final##accountnumber##accounttype_final##branch##amount_final##date_final#">






       <pre> 
       #accountholder#<br>
       #accountnumber#<br>
       #accounttype#<br>
       #bankname#<br>
       #branch#<br>
       #amount#<br>
       #date#<br>
       #initial#<br>
       #lname_final#<br />
       #accounttype_final#<br>
       #amount_final#<br />
       #date_final#<br>
       123456789012345678901234567890123456789012345678901234567890<br />
       #total_string#<br>
       <br />
       <br />
       </pre>





       <!---IF FILE FOR BANK EXISTS--->
        <cfif FileExists(ExpandPath("#listgetAt('#index#',5)#.txt"))>

            <!---READ EXISTING FILE HEADER--->
            <cffile action="read" file="C:/ColdFusion10/cfusion/wwwroot/kelly2/#bankname#.txt" variable="bankheader">
            <!---SPLIT UP THE HEADER TO ADD NEW VALUES ONTO IT--->
            <cfset numericvalue = listfirst(bankheader,chr(13))>
            <cfset numericvalue = #Right(numericvalue, 13)#>

            <cfset RecordCountvalue = #Left(numericvalue, 3)#>
            <cfset RecordCountvalue = #RecordCountvalue# + 1>
            <cfset RecordCountvalue = #NumberFormat(RecordCountvalue,"000")#>
            <cfset RecordCountvalue = #Left(RecordCountvalue, 3)#>
            <cfset RecordCountvalue = #RJustify(RecordCountvalue, 3)#>

            <cfset TotalRecordvalue = #Right(numericvalue, 10)#>
            <cfset TotalRecordvalue = (#TotalRecordvalue# + #amount#) * 100000>
            <cfset TotalRecordvalue = #NumberFormat(TotalRecordvalue,"0000000000")#>
            <cfset TotalRecordvalue = #Left(TotalRecordvalue, 10)#>
            <cfset TotalRecordvalue = #RJustify(TotalRecordvalue, 10)#>

            <!---SET HEADER FOR FILE--->
            <cfset fileheader_bank = "#UCase(bankname)#">
            <cfset fileheader_bank = "#Left(fileheader_bank, 15)#">
            <cfset fileheader_bank = "#LJustify(fileheader_bank, 16)#">
            <cfset newfile_header = "#fileheader_bank##RecordCountvalue##TotalRecordvalue#">

            <pre>
            #numericvalue#<br />
            #RecordCountvalue#<br />
            #TotalRecordvalue#<br />
            #newfile_header#
            </pre>
            <!---APPEND FILE AND ADD UPDATED HEADER--->
            <cfset bankheader = listSetAt(bankheader,1,"#newfile_header#","#chr(13)#")>
            <cffile action="write" 
            fixnewline="no"
            addnewline="no" 
            file="#getDirectoryFromPath(getTemplatePath())#/#listgetAt('#index#',5)#.txt" 
            output="#bankheader#">

            <!---APPEND FILE AND ADD NEW ENTRY--->
            <cffile action = "append"
            fixnewline="no"  
            file = "C:/ColdFusion10/cfusion/wwwroot/kelly2/#listgetAt('#index#',5)#.txt"  
            output = "#total_string#">


        <!---IF FILE FOR BANK DOES NOT EXIST--->
        <cfelse>
            <!---SET HEADER FOR FILE--->
            <cfset fileheader_bank = "#UCase(bankname)#">
            <cfset fileheader_bank = "#Left(fileheader_bank, 15)#">
            <cfset fileheader_bank = "#LJustify(fileheader_bank, 16)#">
            <cfset newfile_header = "#fileheader_bank#001000#amount_final#">

            <!---CREATE NEW FILE WITH BANK NAME--->
            <cffile action = "write"
            fixnewline="no"  
            file = "C:/ColdFusion10/cfusion/wwwroot/kelly2/#listgetAt('#index#',5)#.txt"  
            output = "#newfile_header#">

            <!---APPEND FILE AND ADD NEW ENTRY--->
            <cffile action = "append"
            fixnewline="no"  
            file = "C:/ColdFusion10/cfusion/wwwroot/kelly2/#listgetAt('#index#',5)#.txt"  
            output = "#total_string#">

        </cfif>
</cfloop>
</cfoutput>

I am not sure if it would be better if I start using an Array to do this, if so please advise.

I am not sure if it is possible to check as I insert each row to insert it into the correct place, I have never heard of this before.

I could also loop through the txt files at the end of the process if need be.

解决方案

Read the csv file using cfhttp. The name attribute creates a query object. You can use Q of Q to do your sort and then carry on.

The details are in the documentation for cfhttp.

这篇关于Coldfusion根据txt中的值排序行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆