Re: Long text file manipulation--BIGGER QUESTION
Re: Long text file manipulation--BIGGER QUESTION
- Subject: Re: Long text file manipulation--BIGGER QUESTION
- From: "Arthur J Knapp" <email@hidden>
- Date: Thu, 05 Apr 2001 19:35:34 -0400
>
Date: Thu, 5 Apr 2001 12:10:33 -0400
>
From: bRaD Weston <email@hidden>
>
Subject: Re: Long text file manipulation--BIGGER QUESTION
>
>>Essentially, I just want to read the first line of the file and delete
>
>>it from the file and add another line to the bottom of the file.
>
... But the PINs of each PIN file can only be used once, and the incoming
>
text file is sorted and cannot be resorted by CODE -- which is to say that I
>
am reading from nine different PIN number text files regularly.
>
The PIN files are too large to read into memory because I would need to read
>
in 9 different files, and the speed of FileMaker in the back end would slow me
>
down too much
OK, a number of ideas come to mind, but this one seems simplest:
Just grab you PINs from the end of the PIN file, then shorten the file
with the "set eof" command. If you need to save the PINs that have been
used, copy them out to a separate file.
-- snippits of code, (NOT TESTED!!!)
on getPin( aFile ) -- ie: file "Domestic Pins"
-- Assumes that each pin is 5 characters, followd by a return
--
set readLength to 6
-- Read from -6 for 5, (length of Pin, not including return)
--
set NewPin to read aFile from -readLength for 5
-- Truncate file
--
set eof aFile to ((get eof aFile) - readLength)
return NewPin
Another, more complicated, solution would be to create your own
file structure, something like:
File "Domestic Pins"
+AAAAA
+AAAAB
+AAAAC
+AAAAD
-AAAAE
-AAAAF
-AAAAG
where "+" indicates that this Pin has been used, and where "-"
indicates that the Pin is available.
If AppleScript's "read" had some sort of "seek" function, then you
could just search for the next "-". Unfortunately, doing this:
read (yourFile) until "-"
can cause a memory problem, (you mentioned files on the order of
2 megs). To avoid the memory issue, you could do something like
this:
-- psuedo-code, (NOT TESTED!!!)
-- yourFile = has been opened:
set filePosition to 1
repeat until ((read yourFile for 1 starting at filePosition) = "-")
set filePosition to filePosition + 6 -- for Pin-length = 5 + return
end repeat
write "+" to yourFile starting at filePosition -- mark as used
--
-- Note: "starting at...", we have to back up by 1 byte
set Pin to read yourFile for 5 -- length of Pin.
--
-- Note: File marker is already where we need it from write operation
While the above would avoid memory issues, there would still be the
issue of speed. Reading 6 characters at a time in a large file is
actually quite ridiculous. A better system would be to:
1) Read the file in chunks, (with each chunk evenly divisable by the
Pin-length, [ + return]).
2) Parse the chunk with a scripting addition or in plain AppleScript,
finding your Pin.
3) Add the found offset from 2) to the starting offset of the chunk
in the file.
4) Write the "update" character to that byte position.
In any case, you definately want to avoid the removal of text
from the start or middle of a file, if you can avoid it.
Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden
Hey, check out:
http://www.LateNightSW.com