News:

  • August 24, 2026, 04:39:09 AM

Login with username, password and session length

Author Topic: Case Statement?  (Read 8536 times)

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Case Statement?
« 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?

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6172
  • Yes Pinky, Do-more will control the world!
Re: Case Statement?
« Reply #1 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)]
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Mike Nash

  • Hero Member
  • *****
  • Posts: 652
Re: Case Statement?
« Reply #2 on: March 29, 2020, 10:28:44 AM »
Or try LERP.

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: Case Statement?
« Reply #3 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?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3843
    • Host Engineering
Re: Case Statement?
« Reply #4 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.

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: Case Statement?
« Reply #5 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.