• 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: Nigel Garvey <email@hidden>
  • Date: Tue, 29 Jun 2004 23:35:37 +0100

Emmanuel wrote on Tue, 29 Jun 2004 15:44:52 +0200:

>At 1:20 PM +0100 29/06/04, Martin Orpen wrote:
>>That's given me an idea:
>>
>> set theList to {}
>> repeat with n from 1 to 2499
>> set end of theList to n
>> end repeat
>> set theCount to length of theList
>> set text item delimiters to "*"
>> set theSum to do shell script "echo '" & theList & "' | bc"
>
>That's way less fragile than the "run script" that I proposed.

'theSum' just comes out as an empty string on my 10.2.8 machine and the
process takes longer than 'statlist' on lists that short.

It's possible to write a vanilla 'repeat' equivalent that's just a little
faster than statlist:

on sumAndAverage(theList)
script o
property l : theList
end script

set sum to 0
set listLength to (count theList)
set lenMod10 to listLength mod 10
repeat with i from 1 to lenMod10
set sum to sum + item i of o's l
end repeat
repeat with i from lenMod10 + 1 to listLength by 10
set sum to sum + (item i of o's l) + (item (i + 1) of o's l) + ,
(item (i + 2) of o's l) + item (i + 3) of o's l + ,
(item (i + 4) of o's l) + item (i + 5) of o's l + ,
(item (i + 6) of o's l) + item (i + 7) of o's l + ,
(item (i + 8) of o's l) + item (i + 9) of o's l
end repeat

return {sum:sum, average:sum / listLength}
end sumAndAverage

sumAndAverage({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})
--> {sum:66, average:6.0}

(The lines that end with "," should end with a line-continuation
character.)

NG
_______________________________________________
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>
    • Re: Sum of list?
      • From: Martin Orpen <email@hidden>
  • Prev by Date: Re: Automator in OS 10.4 (Tiger)
  • Next by Date: Need a little help...
  • Previous by thread: Re: Sum of list?
  • Next by thread: Re: Sum of list?
  • Index(es):
    • Date
    • Thread