# Switch Expression

**URL:** <https://forum.inductiveautomation.com/t/switch-expression/62143>\
**Category:** Ignition\
**Created:** [July 20, 2022, 8:01pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143 "2022-07-20T20:01:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Smith](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Michael\_Smith](https://forum.inductiveautomation.com/u/Michael_Smith)\
**Post date:** [July 20, 2022, 8:01pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/1 "2022-07-20T20:01:43Z")

</div>

Hi there. I have a transaction group I’m trying to set up, and in that transaction group I have a expression item. Purpose of this expression item is to read a tag value and assign a string return based on the numeric value.  
Code:

```auto
switch(
{[Rim_Joist]BoardType},
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
"Unknown",
"1X3",
"1X4",
"1X6",
"1X8",
"2X4",
"2X6",
"2X8",
"2X10",
"2X12",
"1X2",
"2X2",
"2X3",
"Unknown",
"Unknown",
"Unknown",
"Unknown",
"Unknown",
"Unknown",
"Unknown", 
"Unknown)

```

This only returns null for a value, even when the tag is a value of 1. No idea why this doesn’t work. Any ideas?

Thanks!  
Edit: that last line 'Unknown" was added, it didn’t work without it either. I thought maybe it needed something there to handle the ‘default’

---

<div class="post-metadata">

**Author:** ![stuart.williams](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/stuart.williams/32/42310_2.png) [@stuart.williams](https://forum.inductiveautomation.com/u/stuart.williams)\
**Post date:** [July 20, 2022, 8:17pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/2 "2022-07-20T20:17:12Z")

</div>

I typically use the case() function. It’s the exact same but the syntax is more readable imo: [case - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/case)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 20, 2022, 8:54pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/3 "2022-07-20T20:54:01Z")

</div>

There isn’t any reason to have the extra unknown lines since that would be your default value upon no match.

```auto
case( {[Rim_Joist]BoardType},
	1, "1X3",
	2, "1X4",
	3, "1X6",
	4, "1X8",
	5, "2X4",
	6, "2X6",
	7, "2X8",
	8, "2X10",
	9, "2X12",
	10, "1X2",
	11, "2X2",
	12, "2X3",
	"Unknown"
)

```

---

<div class="post-metadata">

**Author:** ![Michael\_Smith](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Michael\_Smith](https://forum.inductiveautomation.com/u/Michael_Smith)\
**Post date:** [July 21, 2022, 2:00pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/4 "2022-07-21T14:00:01Z")

</div>

I would agree that the case function is more readable, @stuart.williams but when I hit the little functions button I didn’t see case there, cause I’m blind apparently. 😀

I’ll try it like you put down @dkhayes117 , thanks. Would that extra crap be the reason it doesn’t work, or would it be the formatting? I would expect it should return something the way I wrote it, not null.

---

<div class="post-metadata">

**Author:** ![stuart.williams](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/stuart.williams/32/42310_2.png) [@stuart.williams](https://forum.inductiveautomation.com/u/stuart.williams)\
**Post date:** [July 21, 2022, 2:19pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/5 "2022-07-21T14:19:51Z")

</div>

I agree with dkhayes. Just removing all the extra "unknown"s makes it more readable and also covers a lot of bases with just one line.

Also, if the code in your initial post was copied and pasted exactly, you’re missing the ending double quote on your final unknown. Another thing is you are missing the default case which is required for the switch statement. You have 20 cases with 20 returns, but no default case. That might have been why it was not compiling.

I don’t see the option to view why the expression is failing in the transaction group. My suggestion for troubleshooting in the future would be to create an expression tag and past the expression in it. If there’s a problem with the code, the “Diagnostics” tab on the expression tag will give you more information.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 21, 2022, 2:51pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/6 "2022-07-21T14:51:21Z")

</div>

Pretty sure the reason it wasn’t working was simply that the switch function sucks so bad! 🙄 I used it and hated it when I first started ignition as well, before I found the case statement. There’s really no place for it at all; its only purpose is to confuse and frustrate people with its unnecessary separation of test condition and result. Your example is the golden example that demonstrates this

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 21, 2022, 3:34pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/7 "2022-07-21T15:34:53Z")

</div>

Might have been useful if we’d been able to unpack arrays in expressions, so you could match an array of keys with an array of values ? I don’t know, I hate it too :X

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 21, 2022, 4:08pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/8 "2022-07-21T16:08:48Z")

</div>

> [@pascal.fragnoud](#):
>
> so you could match an array of keys with an array of values

There's a `lookup()` function for that (for datasets).

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [July 21, 2022, 5:04pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/9 "2022-07-21T17:04:56Z")

</div>

I agree. The addition of `case` was much appreciated. `switch` is a great candidate for deprecation.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 21, 2022, 6:24pm UTC](https://forum.inductiveautomation.com/t/switch-expression/62143/10 "2022-07-21T18:24:20Z")

</div>

> [@pturmel](#):
>
> There’s a `lookup()` function for that (for datasets).

If the data is used in more than one spot, it would make sense to put it in a dataset tag and use the lookup function.
