comments inline
> I'm attempting to build an array from .txt file. What I'd
> like to do is create a new element in the array with every
> new line in the text file.
I assume you don't want to skip empty lines in the file...
> Now I'm reading the file like this:
> <cfdirectory directory="H:\cfdev\" action="list"
> name="dirs">
> <cfloop query="dirs">
> <!--- Read that file. --->
> <cffile
> action="read"
> file="H:\cfdev\#dirs.name[count]#"
> variable="filetemp" charset="utf-8">
> <!--- Turn filetemp into array --->
> <cfloop from="1" to="#dirs.recordcount#" index="go">
At this point you've made at least one mistake that will prevent you
from getting the result you want... which is, you're looping from 1 to
the number of files in the directory -- which should (hopefully) have
nothing to do with the length of the file...
> <cfset grab =
> refindnocase(".*\r\n",filetemp,start,"true")>
You don't need quotes around "true" -- it's a literal value by
itself...
> <cfif grab.pos[1] eq 0>
> <cfbreak>
> <cfelse>
If you break here without adding .* to your array, you'll drop the
last line in your file.
> <cfset numb[go] =
> mid(filetemp,grab.pos[1],grab.len[1])>
> <cfset filetemp = removechars(filetemp, 1,
> grab.len[1])>
> </cfif>
> </cfloop>
> </cfloop>
I don't see where the array numb[] is created... if it's created above
the outside loop, then each iteration of the inside loop (file) will
overwrite some or all of the elements in the array from the previous
iteration (file).
> But the issue I'm having is with the RegEx. For some
> reason CFFile strips out the line feed characters. Anyone
> have a work around, or a Custom Tag for this?
There may be some confusion here because the code sample you gave
deals with multiple files... On the other hand, if you're using an
older version of ColdFusion, then \r\n may not be available in your
regular expressions. The regular expression engine in CF5 as I recall
only supports the posix character classes... for that matter, I'm not
sure \r\n are valid in CFMX or CF7 ... I know CFMX implemented a
number of these, but I don't think they're identical to the ones
available in JavaScript / ECMA / ActionScript. You may have better
luck with using #chr(13)##chr(10)# in your regular expression instead
of \r\n. Plus, if you want to make sure it's cross-platform, you need
to make it a class or subexpression and count the number of them... I
believe only windows uses both, max, linux/*nix use a single
character, but mac doesn't use the same one the *nix systems use...
It might be more efficient to replace any empty lines with a single
space and then use listToArray() to convert the file into an array...
this should work:
<!--- convert all windows line breaks --->
<cfset filetemp = replace(filetemp,chr(13) & chr(10),chr(10),"ALL")>
<!--- convert all mac? line breaks --->
<cfset filetemp = replace(filetemp,chr(13),chr(10),"ALL")>
<!--- convert all double-line breaks --->
<cfset filetemp = replace(filetemp,chr(10) & chr(10),chr(10) & " " &
chr(10),"ALL")>
<!--- convert the file to an array --->
<cfset lineArray = listToArray(filetemp,chr(10))>
hth
s. isaac dealey 954.522.6080
new epoch : isn't it time for a change?
add features without fixtures with
the onTap open source framework
http://www.fusiontap.com
http://coldfusion.sys-con.com/author/4806Dealey.htm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Discover CFTicket - The leading ColdFusion Help Desk and Trouble
Ticket application
http://www.houseoffusion.com/banners/view.cfm?bannerid=48
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:212268
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54