Re: Reversing the lines in a file
Re: Reversing the lines in a file
- Subject: Re: Reversing the lines in a file
- From: Paul Skinner <email@hidden>
- Date: Fri, 10 May 2002 09:57:51 -0400
Why do you want to go and do all that thrashing around?
The version I posted does none of that and reads, reverses, and writes a
text file with >25000 lines in under 10 seconds.
On Friday, May 10, 2002, at 06:08 AM, has wrote:
Mr Tea wrote:
I'm ducking now, in anticipation of a barrage of faster
and more efficient ways to do this.
The culprit, extracted:
set theNewText to ""
repeat with thePara in theFlipper
set theNewText to theNewText & thePara & return
end repeat
Mr Tea! No more Darjeeling for you! Go to the blackboard this instant
and
write out 100 times "I must use TIDs more often!"
--
Here's a mangled version of Mr Tea's code that addresses both the above
design flaw *and* the stack overflow errors that are caused when there's
more than approx 4000 paragraphs in the string:
======================================================================
property everyItemLib : load script (choose file with prompt "Please
[NO-BREAK]locate everyItemLib:" of type {"osas"})
on reverseFile(theFile)
try
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
--
open for access theFile with write permission returning fileRef
set theText to read fileRef
tell everyItemLib to set paragraphList to
[NO-BREAK]everyParagraph(theText)
set theText to (reverse of paragraphList) as string
set eof fileRef to 0
write theText to fileRef
close access fileRef
--
set AppleScript's text item delimiters to oldTID
on error errmsg
set AppleScript's text item delimiters to oldTID
try
close access fileRef
on error
end try
display dialog errmsg
end try
end reverseFile
--
on open theFiles
repeat with theFile in theFiles
reverseFile(theFile)
end repeat
end open
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
Requires the everyItemLib library from my site. (Every scripter should
have
this...)
One important note: this script does all the text munging in-memory. In
OS8/9 you'll need to up the memory allocation for the applet to allow
for
this as large text files will soon chew through the MBs. [1]
has
[1] It's perfectly possible to write a routine that keeps memory
consumption way down by reading a line at a time from one file to
another,
but it's slower and more complicated. In these days of half-gig RAM,
it's
rarely worth the bother.
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta
AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.
--
Paul Skinner
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.