MACSCRPT Archives

November 2012

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
John Delacour <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Tue, 13 Nov 2012 21:56:36 +0000
Content-Type:
text/plain
Parts/Attachments:
text/plain (50 lines)
On 13/11/2012 21:15, Bill Steele wrote:

> set keywords to {"foo","bar","Fred"}
> set thePieces to splittext bigFile using "whatever"
> repeat with onePiece in thePieces
> 	repeat with oneWord in keywords
> 		if onePiece contains oneWord
> 			(do some stuff with it)
> 		end if
> 	end repeat
> end repeat
>
> This worked fine until I upgraded to Mountain Lion, and then it started to hang up about halfway through --at different places at different times, with the same file.
>
> ...Seems Applescript Editor needs to stop and think about some things, or maybe clean out some memory when it's doing arrays.
>
> Any ideas what's happening?

On the face of it you are building a huge array from a huge file all in 
memory.  I would do the work with Perl, but at least with Standard 
Additions osax (look at the dictionary) you can read the file line by 
line as you would in C or Perl and not load up the memory.

In the script below I have included the list _lines, but this is only 
for demonstration.  Yu would also remove “with _i from 1 to 10” in order 
to read the whole file line by line, but memory for the current _line 
will be all that is needed.

set _file to POSIX file 
"/Library/Logs/AppleFileService/AppleFileServiceAccess.log"
try
   close access _file
end try
set _fd to open for access _file
set _nl to ASCII character 10 -- or 13 for old files
set _lines to {}
repeat with _i from 1 to 10
   try
     set _line to read _fd until _nl
     -- do something with _line
   on error -- ie. EOF
     return _lines
   end try
   set end of _lines to _line
end repeat
close _fd
return _lines

--JD

ATOM RSS1 RSS2