Hi
Is it possible to have a ‘variable IF’ in a subroutine. If the supplied parameter is L, then the IF should go as:
if a < b then …
and if the parameter is G, then the IF should go as
if a > b then …
Is there a way?
Thanks
Lazybaer
Do you mean something as this?
h(true, 5, 4) --> false
h(false, 5, 4) --> true
to h(x, a, b)
if x then
a < b
else
a > b
end if
end h
Sorry, not quite in that direction.
The following list-sort should work in ascending and descending order. For that purpose, the IF-Statement should have the compare either < oder >, without too many additional statements.
set raus to false
repeat
set ctrl to true
repeat with y from 1 to (count of sinn) - 1
if item y of sinn > item (y + 1) of sinn then – <----- this IF-Statement
set zwi to item (y + 1) of sinn
set item (y + 1) of sinn to item y of sinn
set item y of sinn to zwi
set ctrl to false
end if
end repeat
if ctrl then set raus to true
if raus then exit repeat
end repeat
Thanks anyway
Lazy
Something like this:
if test(3, 4, false) then
beep 1
else
beep 3
end if
to test(x, y, d) -- d is boolean
if d then return x > y
return y > x
end test
Actually jj supplied just what you needed. If efficiency isn’t important but generality is, then something like
set raus to false
repeat
set ctrl to true
repeat with y from 1 to (count of sinn) - 1
if compare(item y of sinn, item (y + 1) of sinn) then -- <----- this IF-Statement
set zwi to item (y + 1) of sinn
set item (y + 1) of sinn to item y of sinn
set item y of sinn to zwi
set ctrl to false
end if
end repeat
if ctrl then set raus to true
if raus then exit repeat
end repeat
on compare(x, a, b)
if x then return a > b
return a < b
end compare
should work.
Just a comment (or two): for the sort to work properly it should be two nested passes through it, i.e.,
repeat with i from 1 to (count of theList) - 1
repeat (count of theList) - i times
-- do compare of index i and i + 1
end repeat
end repeat
or something like that - I might be off a little. Also, notice the lines
if ctrl then set raus to true
if raus then exit repeat
can just be combined to
if ctl then exit repeat
I know you didn’t ask, but…
- Dan
Oops, as usual I forgot something.
The compare() call should be
compare(x, item y of sinn, item (y + 1) of sinn)
where x is the true/false value passed in by jj’s calling routine h.
Hi all
Thanks to all woh wrote in. My script runs now perfectly and as required.
Regards
Lazy