• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: minimum function?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: minimum function?


  • Subject: Re: minimum function?
  • From: kai <email@hidden>
  • Date: Wed, 6 Apr 2005 03:10:28 +0100


On Tue, 5 Apr 2005 21:53:29 +0100, Rob Stott wrote:

Spooky... that's almost what I said... ;-)

I'm confused about why this script doesn't fall over with a 'list' containing just one item though.

On 5 Apr 2005, at 21:40, Mark J. Reed wrote:

No need to do that; what happens if all the values are > 2^512?
Unlikely, but not impossible. :)
Just start out with it set to the first one and loop through the rest:

on min(theList)
        set minVal to theList's item 1
        repeat with i from 2 to count theList
                if theList's item i < minVal then
                        set minVal to theList's item i
                end if
        end repeat
        return minVal
end min

Then you can call that anywhere else in your code like so:

set smallest to min({5,8,17,23,6});


The "repeat with <loopVariable> from <startValue> to <stopValue>" kind of repeat loop terminates when the value of the variable is greater* than a predefined stop value. (* Or smaller - depending on the direction of value change.) Consider this simple example:

--------------

set startValue to 1
set stopValue to 3

set valueList to {}
repeat with currentValue from startValue to stopValue
	set valueList's end to currentValue
end repeat

valueList
--> {1, 2, 3}

--------------

When 'currentValue' reaches 4, it is greater than 'stopValue' (3), so the loop is terminated before the iteration is executed.

It's obviously a similar situation when the variable decreases, except that the loop is discontinued as soon as 'currentValue' is less than 'stopValue':

--------------

set startValue to 8
set stopValue to 6
set stepValue to -1

set valueList to {}
repeat with currentValue from startValue to stopValue by stepValue
	set valueList's end to currentValue
end repeat

valueList
--> {8, 7, 6}

--------------

Even when a step value has an incremental value of greater than 1, the same rules are applied. In such a situation, the variable may sometimes exceed the stop value without actually hitting it:

--------------

set startValue to 3
set stopValue to 9
set stepValue to 2

set valueList to {}
repeat with currentValue from startValue to stopValue by stepValue
	set valueList's end to currentValue
end repeat

valueList
--> {3, 5, 7, 9}

--------------

The next value in the sequence would, of course, have been 11. Since that's larger than the stop value (10), the loop is terminated at that point, leaving 9 as the last actionable value.

So how does this answer the question? Well, when the start value is *already* greater than the stop value, the loop is terminated immediately - even before the first iteration is executed...

--------------

set startValue to 3
set stopValue to 1

set valueList to {}
repeat with currentValue from startValue to stopValue
	set valueList's end to currentValue
end repeat

valueList
--> {}

--------------

Given the logic outlined above, it might be easier to see now why it doesn't actually error.

It can be easy to miss a trick with this feature, and it's not unknown for some scripts to include a redundant 'if/then' statement before such a loop:

--------------

set listCount to count someList
if listCount > 1 then
	repeat with currentValue from 1 to listCount
		-- do something with currentValue
	end repeat
end if

--------------

However, this version will work just fine - even if the list is completely empty:

--------------

repeat with currentValue from 1 to count someList
	-- do something with currentValue
end repeat

--------------

Other forms of AppleScript's repeat statement are equally tolerant, too. See, for instance, how many beeps or errors this little lot produces:

--------------

repeat 0 times
	beep
end repeat

repeat while false is true
	beep
end repeat

repeat until 1 > 0
	beep
end repeat

repeat with listItem in {}
	beep
end repeat

--------------

Incidentally, I don't think the "repeat with <loopVariable> in <list>" form of the repeat statement has been included yet in the suggestions for a minimum function. Since it allows a slightly more condensed syntax in this type of situation, it might also be worth considering:

--------------

on min(l)
	set m to l's item 1
	repeat with i in rest of l
		if i < m then set m to i
	end repeat
	m's contents
end min

min({27, 9, 3, 19, 5, 8})
--> 3

--------------

---
kai

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: minimum function?
      • From: Martin Orpen <email@hidden>
    • Re: minimum function?
      • From: Rob Stott <email@hidden>
  • Prev by Date: Re: Merge PDFs
  • Next by Date: Re: Mac vs unix Carriage returns on a text file format
  • Previous by thread: Re: minimum function?
  • Next by thread: Re: minimum function?
  • Index(es):
    • Date
    • Thread