0 votes
by (940 points)
edited by

Hi everyone! I've got a small problem. I was trying to find the max of an array and I sort of did, but with a small setback.

(set: $a to (a: 15, 7, 14, 3, 6, 1, 24, 4, 11))

(for:  each _item, ...$a)
[(set: $max to (find: _num where _num > $max, ...$a)'s (1))]

(print: $max)

Correctly prints 24, but also prints: 

I can't find a (1) data name in an array

I tried to access a value in a string/array/datamap, but I couldn't find it

Nine times, so every loop.

 

What am I missing here?

1 Answer

+1 vote
by (1.6k points)
selected by
 
Best answer

Looks like it's hanging up on the " 's (1)" notation, and trying to access an element by that name rather than an element position.

Using "find" in this manner seems a bit inefficient to me. (You're looking at each number, then searching the entire array for other greater numbers each time). My suggestion would be to use an "if" statement in the loop, and grabbing a larger number when it appears.

(set: $a to (a: 15, 7, 14, 3, 6, 1, 24, 4, 11))

(set: $max to 0)
(for:  each _item, ...$a)[
    (if: _item > $max)[(set: $max to _item)]
]
(print: $max)

 

by (940 points)
Ehhh you're right. It is a very inefficient way of doing it. It was my first time trying to create a loop and I wasn't sure how to work it out well.

 

Thanks for the help!
...