• 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: Setting contents of a large file to a variable.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting contents of a large file to a variable.


  • Subject: Re: Setting contents of a large file to a variable.
  • From: Andrew Oliver <email@hidden>
  • Date: Thu, 25 Nov 2004 00:26:03 -0800

On 11/24/04 11:41 PM, "Kumar Shailove" <email@hidden> wrote:

> Hi list,
> I need to manipulate text contained in a very large text file ~ 30-40 MB for
> this I have set the contents of the file to a variable and then manipulate
> that variable and once the variable has been manipulated, I again save the
> file.
> I have written following script for that:
>
> Set x to open for access "myfile.txt"
> Set myText to read x as text
> Close access x
>
> But it gives me the error: Out of Memory.
>
> Any alternative methods to do it?
>

The easiest change to your existing script would be to process the file in
chunks.

The 'read file' accepts optional parameters 'for <double int>' and 'from
<double int>'.

The idea is that you keep a counter as to how much of the file you've read
and iterate through, incrementing the 'from' counter as you go. For example:

 set chunkSize to 1024 * 1024 -- 1 megabyte, adjust as necessary.
 set done to false
 repeat until done
   try
     set myText to read x as text for chunkSize
     doSomethingTo ( myText )
   on error
      -- close to the end, get the last chunk
    set myText to read x as text
     doSomethingTo ( myText )
     set done to true
   end try
 end repeat

'read' will keep track of the last position read and automatically start at
that point, so you don¹t need to maintain your own counter.

The only caveats would be if either the data you were manipulating was split
across chunks, or if file size is an exact multiple of chunkSize in which
last the last iteration (in the 'on error' block) may fail, but hopefully
this will get you on the right track.

Andrew
:)

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Setting contents of a large file to a variable.
      • From: Rob Stott <email@hidden>
References: 
 >Setting contents of a large file to a variable. (From: Kumar Shailove <email@hidden>)

  • Prev by Date: Re: Unicode-to-string bug?
  • Next by Date: Re: Setting contents of a large file to a variable.
  • Previous by thread: Setting contents of a large file to a variable.
  • Next by thread: Re: Setting contents of a large file to a variable.
  • Index(es):
    • Date
    • Thread