Switch Expression

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:

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’

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

2 Likes

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

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"
)
3 Likes

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. :grinning:

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.

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.

4 Likes

Pretty sure the reason it wasn’t working was simply that the switch function sucks so bad! :roll_eyes: 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

5 Likes

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

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

3 Likes

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

1 Like

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.

4 Likes