Re: stack overflow error processing text file [relsit: can't
Re: stack overflow error processing text file [relsit: can't
- Subject: Re: stack overflow error processing text file [relsit: can't
- From: Deivy Petrescu <email@hidden>
- Date: Wed, 18 Jul 2001 13:14:34 -0400
At 9:55 PM -0400 7/17/01, email@hidden wrote:
On Tue, 17 Jul 2001 19:24:23 -0400,
monk <email@hidden> asked,
This isn't a good way to use a recursive call, and using it is what is blowing
out your stack. The actual error occurs when you call the 'random number'
scripting addition command, but only because all the recursive calls to mix()
have eaten up all the stack.
Just put the body of the handler in a "repeat" loop, and you won't use any
stack.
If you are working through a list, use iteration. If you are
working through a
tree, use recursion.
--
Scott Norton Phone: +1-703-299-1656
You are absolutely right. The recursive call here is unnecessary,
ineffective and longer.
This works!
<script>
try
set _source to open for access (choose file with prompt
"choose source to mix:")
set mtext to (read _source) as text
close access _source
set _mix to open for access file (((path to desktop) as
string) & "mixed") with write permission
mix(mtext, _mix)
on error
close access _source
close access _mix
end try
close access _mix
on mix(mytext, _mixed)
set pcount to count of text items of mytext
repeat until pcount < 1
set text item delimiters to return
set mytext to text items of mytext
set k to random number from 1 to pcount
write (contents of item k of mytext & return)
starting at eof to _mixed
set item k of mytext to {}
set mytext to every string of mytext
set pcount to pcount - 1
end repeat
set text item delimiters to ""
set mytext to ""
return 0
end mix
</script>
Deivy Petrescu
http://www.dicas.com