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

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.

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)

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 ?

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)

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

this:

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

can be written like this:

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.

2 Likes

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:

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

if user_input in tagPaths.getColumnAsList(0):

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

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

Yes it is constant in the script

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

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:

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

Yes I also tested it with

 if event.source.text==identification:

instead of

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

and it works (even with the for-loop)

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

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

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

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))

just continue on this topic if its about the same

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

1 Like

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?

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?

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?

Yes the values are as expected.