Which position in Label changed to what?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

I have a Label showing a couple of numbers, which change occasionally. Each time a change takes place, I’d like to check which of the digits actually changed and what digit replaced the old one.

For example, when the Label shows „10836“ and then changes to „10796“, I’d like to trigger the Signals „position 2 changed to 9“ and „position 3 changed to 7“ (approaching from the right because there might be shorter or longer numbers).

I’m thinking of maybe extracting each digit, storing it on its own and then compare on the next change… but maybe there’s a simpler way?
Any suggestions are much appreciated.

:bust_in_silhouette: Reply From: skysphr

Something like this?

var old: int
var new: int
# populate your ints
var pos = 0
while(old or new):
    if old % 10 != new % 10:
        print("Digit %s changed from %s to %s" % [pos, old % 10, new % 10])
    old /= 10
    new /= 10
    pos += 1

Something like this!

I put this in its own function which is called right after the “new”-Label changed. Right after that an “old”-Label is filled with the numbers, so the comparison works on the next ping…

Very elegant, thank you very much!

pferft | 2021-09-16 13:48