• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Reversing the lines in a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reversing the lines in a file


  • Subject: Re: Reversing the lines in a file
  • From: has <email@hidden>
  • Date: Fri, 10 May 2002 11:08:42 +0100

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.

  • Follow-Ups:
    • Re: Reversing the lines in a file
      • From: Mr Tea <email@hidden>
    • Re: Reversing the lines in a file
      • From: Paul Skinner <email@hidden>
  • Prev by Date: Re: AS verbosity and ease(?) of learning
  • Next by Date: Re: AS verbosity and ease(?) of learning
  • Previous by thread: Re: Reversing the lines in a file
  • Next by thread: Re: Reversing the lines in a file
  • Index(es):
    • Date
    • Thread