# BUG?: Strange behaviour while comparing two strings in component event script

**URL:** <https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877>\
**Category:** Ignition\
**Tags:** scripting, vision, ignition81\
**Created:** [May 3, 2024, 8:35am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877 "2024-05-03T08:35:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 3, 2024, 8:35am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/1 "2024-05-03T08:35:42Z")

</div>

Hello,  
The code below is a keyPressed event script of a formatted text field.  
I have the problem that the If condition inside the for-loop below is always being executed. I printed the values and there are definitely not the same. So I don't understand what is happening here or am I overlooking something? Thats the whole script for the moment. After the for loop, I later want to check if flowVar is still false and then set the background color of my formatted text field to red, to indicate that the entered ID was not found.

```python
if event.keyCode==event.VK_ENTER:
    user_input=event.source.text 
	folderPath=event.source.parent.dataPath  
	folderArchiv = folderPath + "/AlarmData/alarmArchiv"    
	tableName="Alarm_" + str(folderPath.rsplit("/", 1)[1])
	start= system.gui.getWindow("Standard Window/Alarms").getRootContainer().getComponent("Cal_Start").date
	end= system.gui.getWindow("Standard Window/Alarms").getRootContainer().getComponent("Cal_End").date	
	count_result=0
	duration_result=0
	flowVar=False

	forderTagPaths=str(folderPath) +"/AlarmData/alarmList"
	tagPaths=system.tag.readBlocking(forderTagPaths)[0].value
	
	for x in range(tagPaths.rowCount):
		identification = tagPaths.getValueAt(x,0) 
		if user_input==identification:
			count_result=system.db.runNamedQuery("countStop", {"tableName":tableName, "start":start, "end":end , "user_input":user_input})
			duration_result = system.db.runNamedQuery("calculateDurationStop", {"tableName":tableName, "start":start, "end":end , "user_input":user_input})
			if duration_result==None:
				duration_result=0
			event.source.parent.getComponent("lblnumberStops").text=str(count_result)
        	event.source.parent.getComponent("lbltotalDur").text=shared.Func_Dataset.format_duration(duration_result)
        	flowVar=True
        	system.gui.messageBox(user_input)
        	system.gui.messageBox(identification)

```

---

<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:** [May 3, 2024, 8:46am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/2 "2024-05-03T08:46:44Z")

</div>

I can't see why this would happen, but there are things in this script that don't make much sense to me.  
The whole loop thing, actually.

The execution of the code inside depends on a comparison with `user_input`, which doesn't ever change.  
This makes the loop pointless.

Can you explain what you want to do ? Is the whole thing about coloring a text field if some value was not found ?

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 3, 2024, 8:53am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/3 "2024-05-03T08:53:44Z")

</div>

Of course the input changes. That is an input component, so each time the user type an ID and valide it through ENTER the script get executed. The users can enter what ever the want as as it is numbers and the lengh is 13 (validation Mode: regular expression)

---

<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:** [May 3, 2024, 8:56am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/4 "2024-05-03T08:56:09Z")

</div>

But it doesn't change IN the script. Which makes comparing to it in a loop pointless.

this:

```python
for x in range(tagPaths.rowCount):
	identification = tagPaths.getValueAt(x,0) 
	if user_input==identification:

```

can be written like this:

```python
if user_input in tagPaths.getColumnAsList(0):

```

No need for a loop. Unless the input can be in in `tagPaths` more than once ?  
Actually, even if it appears more than once, the exact same operations would get repeated, which is pointless.

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 3, 2024, 9:01am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/5 "2024-05-03T09:01:37Z")

</div>

No the input can not be in the dataset more than once. Thank you for the getColumnAsList(). I will try it without for loop.

The input changes in the script:

> [@Om33](#):
>
> ```python
> if event.keyCode==event.VK_ENTER:
> user_input=event.source.text 
> 
> ```

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 3, 2024, 9:04am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/6 "2024-05-03T09:04:53Z")

</div>

It works with this instead of the for loop. Thank you. But how to explain that behaviour?

```python
if user_input in tagPaths.getColumnAsList(0):

```

---

<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:** [May 3, 2024, 9:05am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/7 "2024-05-03T09:05:10Z")

</div>

It's assigned once in the script, but it doesn't change afterward.  
You can consider it a constant in that script.

---

<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:** [May 3, 2024, 9:05am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/8 "2024-05-03T09:05:45Z")

</div>

What do you mean, how to explain this ?  
It's just checking if the value is in the list.

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 3, 2024, 9:05am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/9 "2024-05-03T09:05:56Z")

</div>

Yes it is constant in the script

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 3, 2024, 9:06am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/10 "2024-05-03T09:06:55Z")

</div>

I mean why is it not working when using a for-loop? It shouldn't be a problem

---

<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:** [May 3, 2024, 9:11am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/11 "2024-05-03T09:11:14Z")

</div>

I'm not sure but there are a few weirdnesses with assigning components content to variables in scripts.  
I've been burn by terribly counter-intuitive behavior, but I don't think it applies there.

Maybe someone else will have an answer for you...

here:

> [@\[IGN-1660\]PSA - Assigning values to a property in a script may drive you nuts](https://forum.inductiveautomation.com/t/ign-1660-psa-assigning-values-to-a-property-in-a-script-may-drive-you-nuts/76192):
>
> Hello folks, This got me pulling the hair out of my head. I spent the last hour yelling at my screen while doing all sorts of tests and experiments. I think I got it now, but... try it yourself. Here's the json for a perspective view: 
> 
> > ****
> >
> > { "custom": {}, "params": { "category\_id": "", "family\_id": "", "item\_id": "" }, "propConfig": { "custom.isFresh": { "access&q…

Edit:  
Actually it does apply there.  
If something changed the value in your input component...  
You'd expect the input to be a constant, but it's actually not.  
But there's no reason it would work with the `in` version...

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 3, 2024, 9:19am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/12 "2024-05-03T09:19:31Z")

</div>

Yes I also tested it with

```python
 if event.source.text==identification:

```

instead of

```python
user_input=event.source.text
if user_input==identification:

```

and it works (even with the for-loop)

---

<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:** [May 3, 2024, 9:20am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/13 "2024-05-03T09:20:56Z")

</div>

Yes you should probably use the full path, even without the loop.  
And maybe for the other variables as well.

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 3, 2024, 9:22am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/14 "2024-05-03T09:22:33Z")

</div>

Okay thank you, at least I can continue my work now.

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 3, 2024, 9:48am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/15 "2024-05-03T09:48:10Z")

</div>

This problem is really driving me crasy. It doesn't work anymore. The same problem still persists. I have to finish work now. I will open a new topic on monday. Thats how the script looks now

```python
if event.keyCode==event.VK_ENTER:
	folderPath=event.source.parent.dataPath  
	folderArchiv = folderPath + "/AlarmData/alarmArchiv"    
	tableName="Alarm_" + str(folderPath.rsplit("/", 1)[1])
	start= system.gui.getWindow("Standard Window/Alarms").getRootContainer().getComponent("Cal_Start").date
	end= system.gui.getWindow("Standard Window/Alarms").getRootContainer().getComponent("Cal_End").date	
	count_result=0
	duration_result=0
	flowVar=False

	forderTagPaths=str(folderPath) +"/AlarmData/alarmList"
	tagPaths=system.tag.readBlocking(forderTagPaths)[0].value
	
	if event.source.text in tagPaths.getColumnAsList(0):	
		count_result=system.db.runNamedQuery("countStop", {"tableName":tableName, "start":start, "end":end , "user_input":event.source.text})
		duration_result = system.db.runNamedQuery("calculateDurationStop", {"tableName":tableName, "start":start, "end":end , "user_input":event.source.text})
		if duration_result==None:
			duration_result=0
		event.source.parent.getComponent("lblnumberStops").text=str(count_result)
        event.source.parent.getComponent("lbltotalDur").text=shared.Func_Dataset.format_duration(duration_result)
        flowVar=True
        system.gui.messageBox("test")
    system.gui.messageBox(str(flowVar))

```

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [May 3, 2024, 11:33am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/16 "2024-05-03T11:33:07Z")

</div>

> [@Om33](#):
>
> I will open a new topic on monday

just continue on this topic if its about the same

> [@Om33](#):
>
> `system.gui.messageBox("test")`

instead of printing test, print the event.source.text and tagPaths.getColumnAsList(0) to be sure they are what you expect them to be 🙂

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 6, 2024, 4:32am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/17 "2024-05-06T04:32:08Z")

</div>

I have also printed the value of `event.source.text` and the value is always the same that I enter in the formatted text field. And it is also easy to know that a value is not available in my IDs database because all the IDs start with (just an example) 00000 and I will enter something starting with 22222 in the field. I have also printed the value of `flowVar` and it is always True.

Maybe I should mention that the script is in an inheritable project?

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 6, 2024, 6:50am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/18 "2024-05-06T06:50:19Z")

</div>

Update:  
when I remove the `event.keyCode==event.VK_ENTER:` at the beginning of script, it works. Also when I move the script to a mouseClicked event for example. It sounds absurd, but maybe the problem is the if-loop inside the if-loop?

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [May 6, 2024, 7:07am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/19 "2024-05-06T07:07:44Z")

</div>

Have you printed out **both** variables of the if, to be sure they are what you expect them to be? are you grabbed the correct column?

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [May 6, 2024, 7:11am UTC](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877/20 "2024-05-06T07:11:46Z")

</div>

Yes the values are as expected.

[Next page](https://forum.inductiveautomation.com/t/bug-strange-behaviour-while-comparing-two-strings-in-component-event-script/89877.md?page=2)
