Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: RBPLC on March 29, 2020, 09:04:30 AM

Title: Case Statement?
Post by: RBPLC on March 29, 2020, 09:04:30 AM
I'm trying to use the following in a Math Expression: IF(CT0.ACC==0,1565,IF(CT0.ACC==1, 1559, IF(CT0.ACC==2, 1560, IF(CT0.ACC==3, 1561, IF(CT0.ACC==4, 1562, IF(CT0.ACC==5, 1563, 1564)))))). I was trying to use one expression but apparently this exceeds the math stack depth. I don't particularly like all of the nested if statements anyway. Is there a better way to do this in one expression (something similar to a Case Statement) or do I just need to use a math expression for each of the cases?
Title: Re: Case Statement?
Post by: BobO on March 29, 2020, 10:01:02 AM
Create a small memory block. Pre-fill with the numbers. Use a single statement: Result = Block[min(CT0.Acc, 6)]
Title: Re: Case Statement?
Post by: Mike Nash on March 29, 2020, 10:28:44 AM
Or try LERP.
Title: Re: Case Statement?
Post by: RBPLC on March 29, 2020, 10:46:37 AM
Using LERP like that is pretty slick. Would there be an equivalent way to do that if the outputs were strings instead of integers?
Title: Re: Case Statement?
Post by: franji1 on March 29, 2020, 11:10:07 AM
Using LERP like that is pretty slick. Would there be an equivalent way to do that if the outputs were strings instead of integers?
It already exists in the STRPRINT script language.  Lookup(value, 0string, 1string, 2string, ... Nstring) where Nstring is returned when value does not equal 0..N-1, otherwise it returns the string at that idex.

Each string can be a string literal or string element.
Title: Re: Case Statement?
Post by: RBPLC on March 29, 2020, 12:04:26 PM
Thanks guys. Those are two pretty nifty tools and do exactly what I wanted in a more concise manner.