Notifications
Clear all

[Closed] Sum Function?

Is there a sum function I’m not finding or do I need to include it in my custom script library?

such as:

SumArray #(1,2,3,4)
results: 10

4 Replies
1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

Since an array can contain anything and not just one type of elements, a SUM function wouldn’t make much sense in most cases. Plus, a simple for loop can deal with it nicely when you need the sum…
Write your own

Well I know at least PHP has some nice little array functions such as

ArrayDifference (which works on all data types) aka #(“A”, 3, 2.5) #(3, “A”, 2.6) returns #(“A”, 3)

and Array Sum amongst others.

It would be nice if they were C++ and not scripted for speed.

1 Reply
(@zeboxx2)
Joined: 11 months ago

Posts: 0

On the other hand, it uses . as a concatenation character, believes ‘string’ and “string” are two different beasts entirely, believes whitespace is a perfectly valid way to define scope and oh yes, don’t ever forget a semicolon because a cr/lf just won’t do.

But yes, it does have nice array functions; but then a PHP array isn’t really an array either

Well, you could try dotnet – but overall, summing an array of numbers in maxscript shouldn’t be particularly slow?

sumVal = 0; for val in <array> do ( sumVal += val )

For a random array of 1,000,000 integers, summing took 1.823 seconds on my machine… can only imagine what it’d do on a faster machine (single core, of course). I’m sure it’ll be faster in C, possibly even faster in ASM if the compiler does a less-than-great job, but just how many are you summing that it becomes a concern?

Fair enough. (although all the more reason Maxscript which I like so much more should have a few of the little perks that even php has.)

I guess my random wish is: “It would be nice if it shipped with Max.”