# Using python dictionary (switch statement) to display string

**URL:** <https://forum.inductiveautomation.com/t/using-python-dictionary-switch-statement-to-display-string/9326>\
**Category:** General Discussion\
**Created:** [November 12, 2014, 6:52pm UTC](https://forum.inductiveautomation.com/t/using-python-dictionary-switch-statement-to-display-string/9326 "2014-11-12T18:52:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pstojakovic](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@pstojakovic](https://forum.inductiveautomation.com/u/pstojakovic)\
**Post date:** [November 12, 2014, 6:52pm UTC](https://forum.inductiveautomation.com/t/using-python-dictionary-switch-statement-to-display-string/9326/1 "2014-11-12T18:52:44Z")

</div>

I have a integer tag 0-9 and want to display a string that corresponds to the integer.  
How would I do that using a switch statement…  
so when tag is 0, it should say zero, when changes to 1 should say one…and so on.

Thanks.

---

<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:** [November 12, 2014, 7:25pm UTC](https://forum.inductiveautomation.com/t/using-python-dictionary-switch-statement-to-display-string/9326/2 "2014-11-12T19:25:48Z")

</div>

[size=150]Option 1: Bind the Text Property of the label to an expression:[/size]

`switch({Test/IntergerTag},
	0,1,2,3,4,5,6,7,8,9,
	"zero","one","two","three","four","five","six","seven","eight","nine",
	"ERROR"
	)`

[size=150]Option 2: If you’re really, really looking to use a python dictionary:[/size]  
I created a shared script called convert. in it:

`def int2Name(value):
  dict={0:"zero", 1:"one", 2:"two", 3:"three", 4:"four", 5:"five", 6:"six", 7:"seven", 8:"eight", 9:"nine"}
  if value in dict:
    return dict[value]
  else:
    return "ERROR"`

EDIT: Cleaned up script to make dictionary a bit more manageable. 😉

Then bind the Text Property to the expression:

```auto
runScript(concat("shared.convert.int2Name(",toStr({Test/IntergerTag}),")"),500)
```

---

<div class="post-metadata">

**Author:** ![pstojakovic](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@pstojakovic](https://forum.inductiveautomation.com/u/pstojakovic)\
**Post date:** [November 12, 2014, 7:57pm UTC](https://forum.inductiveautomation.com/t/using-python-dictionary-switch-statement-to-display-string/9326/3 "2014-11-12T19:57:24Z")

</div>

Is referencing allowed in expression binding? Like if I have many paths but referenced by their numbers…

{Folder1/Option1/Selection1/Integer\_Tag} to

{Folder%d/Option%d/Selection%d/Integer\_Tag} % ({folder\_num}, {option\_num}, {selection\_num})

It says it is not valid path for some reason.

---

<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:** [November 12, 2014, 8:18pm UTC](https://forum.inductiveautomation.com/t/using-python-dictionary-switch-statement-to-display-string/9326/4 "2014-11-12T20:18:07Z")

</div>

I’d make a Custom property to handle that portion of it, then reference the custom property in the runscript()

---

<div class="post-metadata">

**Author:** ![pstojakovic](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@pstojakovic](https://forum.inductiveautomation.com/u/pstojakovic)\
**Post date:** [November 12, 2014, 9:38pm UTC](https://forum.inductiveautomation.com/t/using-python-dictionary-switch-statement-to-display-string/9326/5 "2014-11-12T21:38:34Z")

</div>

Well, I am getting confused with that, so I tried using the simplest way just having one Label read the tag and another display the corresponding names from the Label that reads the integers but that too doesn’t work neither.

`if event.source.propertyName == "text":
	target = event.source.parent.getComponent('Display num')
	if event.source.text == 0:
		target.text = "Zero"
	elif event.source.text == "1":
		target.text = "One"
	elif event.source.text == "2":
		target.text = "Two"
	elif event.source.text == "3":
		target.text = "Three"		
	elif event.source.text == "4":
		target.text = "Four"
	elif event.source.text == "5":
		target.text = "Five"
	elif event.source.text == "6":
		target.text = "Six"
	elif event.source.text == "7":
		target.text = "Seven"		
	elif event.source.text == "8":
		target.text = "Eight"
	elif event.source.text == "9":
		target.text = "Nine"`

---

<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:** [November 12, 2014, 9:47pm UTC](https://forum.inductiveautomation.com/t/using-python-dictionary-switch-statement-to-display-string/9326/6 "2014-11-12T21:47:32Z")

</div>

Are you trying to do this in a property binding? I’m just wondering if may be confusing expression language with scripting language. They are separate entities.
