Re: Sum of list?
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.