Re: Help with recursion
Re: Help with recursion
- Subject: Re: Help with recursion
- From: Graff <email@hidden>
- Date: Tue, 11 May 2004 21:30:32 -0400
You aren't passing back the result of your recursive call and so you
are leaving the false information in place. I have commented the
change and it seems to work:
----------
set target_date to get_date()
log "target date is " & target_date
set today to (current date)
set time_remaining to target_date - today
set Weeks_remaining to (time_remaining / weeks) div 1
set days_remaining to ((time_remaining - (Weeks_remaining * weeks)) /
days) div 1 + 1
display dialog Weeks_remaining & " weeks, " & days_remaining & " days."
as text giving up after 5
on get_date()
set target to false
repeat until target is true
set target_date to text returned of (display dialog "enter the target
date" default answer "")
try
set target_date to date target_date
on error
-- used to be:
-- get_date()
set target_date to get_date()
end try
set target to true
end repeat
return target_date
end get_date
----------
I'm not sure why you would use a recursive call here though, why not
just use a loop? You already have a loop in place that is really not
doing anything:
----------
set target_date to get_date()
display dialog "target date is " & target_date
set today to (current date)
set time_remaining to target_date - today
set Weeks_remaining to (time_remaining / weeks) div 1
set days_remaining to ((time_remaining - (Weeks_remaining * weeks)) /
days) div 1 + 1
display dialog Weeks_remaining & " weeks, " & days_remaining & " days."
as text giving up after 5
on get_date()
set target to false
repeat until target is true
set target_date to text returned of (display dialog "enter the target
date" default answer "")
try
set target_date to date target_date
set target to true
end try
end repeat
return target_date
end get_date
----------
No recursion, no mess.
- Ken
On May 11, 2004, at 8:33 PM, Michelle Steiner wrote:
set target_date to get_date()
log "target date is " & target_date
set today to (current date)
set time_remaining to target_date - today
set Weeks_remaining to (time_remaining / weeks) div 1
set days_remaining to ((time_remaining - (Weeks_remaining * weeks)) /
days) div 1 + 1
display dialog Weeks_remaining & " weeks, " & days_remaining & "
days." as text giving up after 5
on get_date()
set target to false
repeat until target is true
set target_date to text returned of (display dialog "enter the
target date" default answer "")
try
set target_date to date target_date
on error
get_date()
end try
set target to true
end repeat
return target_date
end get_date
If a correct entry is made the first time, the script works, but if
there is an error the first time, and the script gets another input,
the results of the first attempt are returned.
I see what the problem is; the incorrect entry is stored, and as the
recursive loop unwinds, the first entry is the one that remains.
How do I fix it?
-- Michelle
--
"The optimist proclaims that this is the best of all possible worlds ;
and the pessimist fears this is true." --James Branch Cabell
_______________________________________________
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.
_______________________________________________
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.