Re: Help with recursion
Re: Help with recursion
- Subject: Re: Help with recursion
- From: Deivy Petrescu <email@hidden>
- Date: Wed, 12 May 2004 10:33:37 -0400
At 5:33 PM -0700 5/11/04, 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.
Michelle;
The problem here is pleonasm, you are using two tools when only one is needed.
You can use only recursion or only a loop.
Furthermore, as Ken pointed out, the recursion is
wrong
Walter suggested a solution with no recursion, only a repeat loop.
The interesting problem comes when we look at the recursion.
According to you:
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 div weeks)
set days_remaining to ((time_remaining mod weeks) div days)
display dialog Weeks_remaining & " weeks, " &
days_remaining & " days." as text giving up after
5
on get_date()
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() -- <-- recursion
end try
return target_date
end get_date
Every time you call get_date() you create another
instance of the the handler that is not connected
with the previous call. So all "target_date" are
different. They are being returned but the one
that sticks is the one returned from the first
call of get_date()
To fix this, you can use 3 methods (that I found out)
1. Ken's suggestion
--
try
set target_date to date target_date
on error
set target_date to get_date() -- <-- recursion
end try
--
Now when there is a returned value for all
get_date() they are going to be sent to the first
call of get_date()'s target_date.
2. Define a global target_date on the handler
--
on get_date()
global target_date
...
end get_date()
--
All the target_dates are the same, when there is
a target date in whichever call of get_date() it
will trickle down (your favorite expression, I
am sure) to the first call.
3. Return your handler call. This way, the script
forgets about all the previous call
--
try
set target_date to date target_date
on error
return get_date() -- <-- recursion
end try
--
Now, if you accept a suggestion with the date part of your script:
set Weeks_remaining to (time_remaining div weeks)
set days_remaining to ((time_remaining mod weeks) div days)
set hours_remaining to ((time_remaining mod days) div hours)
set min_remaining to ((time_remaining mod hours) div minutes)
display dialog Weeks_remaining & " weeks, " &
days_remaining & " days, " & hours_remaining & "
hours, " & min_remaining & " minutes." as text
giving up after 5
--
Regards
Saudagues
Deivy
http://www.dicas.com
_______________________________________________
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.