cancel
Showing results for 
Search instead for 
Did you mean: 

How to validate all element from an array are less than a value?

SOLVED
Frequent Contributor

How to validate all element from an array are less than a value?

Hi,

say we have an array like this

def my_array = [ [45.26], [44.6],[47.25] ] 

def expected = 50 as Float

how do you validate that each element are less than 50

 

i tried this:

assert my_array .every {it < expected }

It throws: 

groovy.lang.GroovyRuntimeException: Cannot compare java.util.ArrayList with value '[45.26]' and java.lang.Float with value '50' error at line: ...

It's comparing this value [45.26] instead of this 45.26

 

i tried this:

assert my_array.max() < expected

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: How to validate all element from an array are less than a value?

Hi @_ivanovich_,

I know youve resolved your issue, but i have a quick question if thats ok for my education really.

In your array each value was separated by individual square brackets.....why was you doing that? I wouldve thought (without testing your code myself) that your original code wouldve worked if you didnt include the square brackets around each value.....???

Nice one,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

View solution in original post

2 REPLIES 2
Frequent Contributor

Re: How to validate all element from an array are less than a value?

Found solution with:

log.info my_array.sum().max()

which returns the max of all array then i have to compare.

please close this thread.

Community Hero

Re: How to validate all element from an array are less than a value?

Hi @_ivanovich_,

I know youve resolved your issue, but i have a quick question if thats ok for my education really.

In your array each value was separated by individual square brackets.....why was you doing that? I wouldve thought (without testing your code myself) that your original code wouldve worked if you didnt include the square brackets around each value.....???

Nice one,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors