• 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: Text file reading... (David Crowe)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Text file reading... (David Crowe)


  • Subject: Re: Text file reading... (David Crowe)
  • From: Paul Berkowitz <email@hidden>
  • Date: Sat, 11 Jan 2003 00:50:52 -0800

On 1/7/03 12:48 AM, "Axel Luttgens" <email@hidden> wrote:

>> Paul suggested reading the whole file and then reading out paragraphs,
>> which don't care about the difference between CR and LF. That leaves
>> the problem, however, of when the file gets too big (I presume 4060
>> bytes is the largest).
>
>
> Not exactly: the file could be very large but, by using Paul's second
> suggestion ('set theLines to paragraphs of r'), you could end with a
> list of strings counting more than about 4060 items. And there's a bug
> in AppleScript when it is asked to produce such lengthy lists that way.

I mentioned that myself. It's very easy to write a routine that gets them in
chunks, and avoids the bug.

try
set theLines to paragraphs of r
on error
set theLines to my LargeTidsList(r, (return))
end try

on LargeTidsList(theText, tid)

local oldTids, newList, a, z, done

set oldTids to AppleScript's text item delimiters
set AppleScript's text item delimiters to {tid}
set {a, z} to {1, 4000}
set newList to {}
set done to false
repeat until done
try
set newList to newList & text items a thru z of theText
set {a, z} to {a + 4000, z + 4000}
on error -- last segment, fewer thn 4000
set newList to newList & text items a thru -1 of theText
set done to true
end try
end repeat
set AppleScript's text item delimiters to oldTids

return newList
end LargeTidsList



But for really enormous text, you occasionally find you can't read the whole
text in at once either. I don't know what the limit is - it's far, far
higher than 32K, but until I discover the limit, I find it simple to use 30K
as the chunk size:



try
set r to read f
on error
set r to my ReadPartials(f)
end try

on ReadPartials(fileRef)

local b, y, r, textClump

set b to 1
set y to 30000

set r to ""

set finished to false

repeat until finished

try
set textClump to read fileRef from b to y
set r to r & textClump
on error -- last text clump
set textClump to read fileRef from b to eof
set r to r & textClump
set finished to true
end try

set {b, y} to {b + 30000, y + 30000}

end repeat

return r

end ReadPartials


--
Paul Berkowitz
_______________________________________________
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.

References: 
 >Re: Text file reading... (David Crowe) (From: Axel Luttgens <email@hidden>)

  • Prev by Date: Re: Safari again
  • Next by Date: Re: Date Help - D&T stamp w Short dates
  • Previous by thread: Re: Text file reading... (David Crowe)
  • Next by thread: path to At Ease
  • Index(es):
    • Date
    • Thread