• 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: Sum of list?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sum of list?


  • Subject: Re: Sum of list?
  • From: Graff <email@hidden>
  • Date: Mon, 28 Jun 2004 21:15:49 -0400

I don't know of any implementations of summation algorithms that don't do some sort of iteration in order to sum up a 1-D array (list) of integers. The best you can do is to keep the loop as simple as possible:
----
set theList to {1, 2, 3}
set theCount to length of theList
set theSum to 0
repeat with i in theList
set theSum to theSum + i
end repeat

set theAverage to theSum / theCount

{theSum, theAverage}
----

If you are dealing with very large amounts of numbers and AppleScript is too slow then you probably should move to another solution. Maybe try the shell tool bc or make your project into a Cocoa-AppleScript project and do the summation in C, calling it from the AppleScript portion.

- Ken

On Jun 28, 2004, at 7:04 PM, John Mistler wrote:

Does anyone know of a way to get the SUM of integers in a list WITHOUT
having to use a repeat loop? I am trying to speed things up . . .

What about getting the AVERAGE of the integers in a list?

i.e. the SUM of {1,2,3} = 6

and the AVERAGE of {1,2,3} = 2
_______________________________________________
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.


  • Follow-Ups:
    • Re: Sum of list?
      • From: Emmanuel <email@hidden>
References: 
 >Sum of list? (From: John Mistler <email@hidden>)

  • Prev by Date: Re: Automator in OS 10.4 (Tiger)
  • Next by Date: launching a script from a Safari (bookmarklet, javascript, schema, etc)
  • Previous by thread: Re: Sum of list?
  • Next by thread: Re: Sum of list?
  • Index(es):
    • Date
    • Thread