Hi dear scripting friends
Today I experienced a real strange ouf memory error while using text item delimiters. The script contains a very well known and common search and replace handler:
on SaR(_atext, _search, _replace)
set oldastid to AppleScript's text item delimiters
set AppleScript's text item delimiters to _search
set _atext to every text item of _atext
set AppleScript's text item delimiters to _replace
set _atext to _atext as string
set AppleScript's text item delimiters to oldastid
return _atext
end SaR
It reads a text file exported as tab delimited text from a database, converts its text items to a list and crawls through the list items in order to perform some search and replaces. It works well with some export files but on one an out of memory error occurs.
All text files have the same format but differ in the count of lines. The script runs without producing errors on export files containing more than 60 lines but on some files containing less than 60 (or more) lines, the out of memory error occurs after the 36th line for example.
ScriptDebugger 4 shows me the error occurs within the SaR() handler. I am not able to find out by what this is caused. What I detected was a var-value showed up as “MEV…8230” in the SD result window but in the original export file there has only “MEV8230” been written.
Might this be related to diacritical marks that will not show up in BBEdit? The export files came from a Windows system.
The SaR handler converts spaces into “;”, “,” or the other way round. There are a lot of SaR() calls in the script but all work except in a special situation, unknown by now.
I checked not to use the same varnames in the handler than in the script’s main body. But this made no difference.
Example code excerpt:
set _stichw_o to stichwoerter of item _liste of _rec
set _stichw to _SaR(_SaR(_ums(_stichw_o), ", ", " "), ",, ", ", ")
set _temp to _SaR(_stichw_o & " " & _stichw, " ", ", ")
set _temp to _SaR(_temp, ", ,", ",")
set _temp to _SaR(_temp, ",", ";")
set AppleScript's text item delimiters to "; "
set _temp to text items of _temp
set AppleScript's text item delimiters to ""
set _temp to _remove_duplicates(_temp)
set _temp to words of _temp
set _tt to ""
repeat with _word in _temp
set _tt to _tt & (_word & "; ") as string
end repeat
-- set _tt to _tt as string
set _temp to (characters 1 through -3 of _tt) as string
set stichwoerter of item _liste of _rec to _temp
set _new_dat to _new_dat & {item _liste of _rec}
There is no difference running the script on Tiger nor Leopard. The out of memory error occurs from both in the same context.
The text processed follows this format:
I cannot understand why it sometimes breaks but with other export files of the same format it does not?! Chars like “°” have been converted into properly chars before processing the list, btw. (see below)
The var _atext is used in the handler only and processes text items in a list stored in a var called _text in the main part of the script.
Really strange. Any ideas?
Best regards,
Thomas
(Chars replaced:)
property _suchumlaute : {"°", "¸", "ˆ", "·", "Æ’", "÷", "¹"}
property _umlaute : {"ä", "ü", "ö", "ß", "Ä", "Ö", "Ü"}