Re: Reversing the lines in a file
Re: Reversing the lines in a file
- Subject: Re: Reversing the lines in a file
- From: Arthur J Knapp <email@hidden>
- Date: Fri, 10 May 2002 14:04:13 -0400
>
Date: Fri, 10 May 2002 01:34:44 +0100
>
Subject: Re: Reversing the lines in a file
>
From: Mr Tea <email@hidden>
>
This from Rice, Gary - dated 10/5/02 12.29 am:
>
>
> I have a text file. Each line in the file ends with a <CR>. It is a large
>
> file. I want to reverse the order of the file. That is, I want the last line
>
> in the file to appear at the begining and the first line in the file to be
>
> at the end.
>
on open theFiles
[snip]
>
set theFlipper to reverse of every paragraph of thetext
[snip]
Good stuff, Mr. Tea. :)
I should have pointed out that the solution I posted was geared towards
the possiblity that a file might have more content then can reasonably
fit into memory. If the file will fit entirely into memory, then one is
really looking for a string-processing solution, not a file-processing
one. So long as one is sure that a file's contents are going to fit into
memory, then all processing can follow this model:
set openFile to open for access ( contentFile ) with write permission
try
(* Read the entire file into memory.
*)
set stringContent to read ( openFile )
(* Process the entire contents as a normal string.
*)
set stringContent to ProcessContent( stringContent ) --> do whatever
(* If the string-processing shortened the contents, then writing
* the file back will leave trailing garbage at the end, so we
* clear the file's contents entirely.
*)
set eof (openFile) to 0
(* Write it back out.
*
* The "starting at" parameter is important, because previous "read"
* operations could have set the file marker anywhere.
*)
write (stringContent) to (openFile) starting at (1)
on error m number n
-- deal with file read/write errors
close access openFile
end
close access openFile
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
<
mailto:email@hidden>
try
<
http://www.facespan.com/>
on error number -128
end try
}
_______________________________________________
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.