Expression: Find Lowest Non-Zero Number?

Hello, I have a set of tags with values, [0,2,0,1].

I need to find the lowest non-zero number from my set in expression language. The min() expression always brings back the zeros.

Any ideas? Thank you.

Hmm… not that it’s any use to you now, but maybe a filter expression function that operates on lists would be a good idea…

Anyway you probably have to do this in script and if you really need the resulting value in an expression for some reason invoke that function via runScript.

Thanks Kevin,

I think I’ve figured it out…

min(
if({[.]tag1}=0,999,{[.]tag1}),
if({[.]tag2}=0,999,{[.]tag2}),
if({[.]tag3}=0,999,{[.]tag3}),
if({[.]tag4}=0,999,{[.]tag4}),
999)

Ah, there you go :slight_smile:

Maybe another cup of coffee will make me smarter.

2 Likes
min(
	max({[.]tag1}, 999),
	max({[.]tag2}, 999),
	max({[.]tag3}, 999),
	max({[.]tag4}, 999),
	999
)

Same effect, less duplicate tag paths (thus less chance for future errors in transcription).

1 Like

Actually, I think that would stomp on the other numbers non zero numbers? anything < 999 would always be 999.

4 Likes

Using the given [0,2,0,1]
this

becomes

min(
	max(0, 999),
	max(1, 999),
	max(0, 999),
	max(2, 999),
	999
)

which becomes

min(999,999,999,999,999)

I wish I was a little more fresh with my math because I feel like this could be done strictly using min/maxes, buts right now its not coming to mind. Doing it in an expression is a little tough, you will definitely need if statements. This might be one of those times where a runScript() is appropriate and do your calculation via scripting.

min(
	if({Tag1}, {Tag1}, None),
	if({Tag2}, {Tag2}, None),
	if({Tag3}, {Tag3}, None),
	if({Tag4}, {Tag4}, None)
)

Lord, these are all ugly. I would definitely use objectScript(), like so:

objectScript("min([999]+[x for x in args if x])", {tag1}, {tag2}, {tag3}, {tag4})

{ Using my free Simulation Aids module, naturally. }

4 Likes

Just noting a lot of these solutions, for a set of [1, -2, -3, -4] would return -4, not 1 - you definitely just want a Non-Zero min, so every other number positive and negative is fair game? Or you want the minimum of non-negative values?

Clarifying because getting a lowest non-negative number is a very common case, but getting a minimum by excluding one specific number from the entire number line, does not seem as common.

Positive values, presumably, as zero is explicitly excluded. My solution becomes:

objectScript("min([999]+[x for x in args if x>0])", {tag1}, {tag2}, {tag3}, {tag4})
2 Likes

In my case, yes, non-negative non-zero #'s, also in my use case there won’t ever be negatives.

1 Like