• Syncronize list with same length beetween 2 Sync objects

    From eddy@21:1/5 to All on Tue Aug 29 07:06:11 2023
    Hello,

    i get from my System following String back
    set info1 {SYNC 1;OP1 4;OP2 4;SYNC 2;OP3 3;SYNC 3;SYNC 4}
    set info2 {SYNC 1;SYNC 2;SYNC 3;OP1 4;OP2 4;SYNC 4}

    The result should be that beetween SYNC 1 and SYNC2 should be the same counter of elements. Placeholder is named e.g NOOP and when OP 1 have a 4 then NOOP should have a 3

    And at the end both lists should have the same length.

    set result1 {SYNC 1;OP1 4;OP2 4;SYNC 2;OP3 3;SYNC 3;NOOP 4;NOOP 4;SYNC 4}
    set result2 {SYNC 1;NOOP 3;NOOP 3;SYNC 2;NOOP 4;SYNC 3;OP1 3;OP2 3;SYNC 4}

    Hope that clear what i mean.

    Can someone help me.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Schelte@21:1/5 to eddy on Tue Aug 29 23:15:41 2023
    On 29/08/2023 16:06, eddy wrote:
    Hello,

    i get from my System following String back
    set info1 {SYNC 1;OP1 4;OP2 4;SYNC 2;OP3 3;SYNC 3;SYNC 4}
    set info2 {SYNC 1;SYNC 2;SYNC 3;OP1 4;OP2 4;SYNC 4}

    The result should be that beetween SYNC 1 and SYNC2 should be the same counter of elements. Placeholder is named e.g NOOP and when OP 1 have a 4 then NOOP should have a 3

    And at the end both lists should have the same length.

    set result1 {SYNC 1;OP1 4;OP2 4;SYNC 2;OP3 3;SYNC 3;NOOP 4;NOOP 4;SYNC 4}
    set result2 {SYNC 1;NOOP 3;NOOP 3;SYNC 2;NOOP 4;SYNC 3;OP1 3;OP2 3;SYNC 4}

    Hope that clear what i mean.

    It isn't clear to me why the "OP1 4" and "OP2 4" from info2 change to
    "OP1 3" and "OP2 3" in result2.

    So I ignored that in my solution below. It may not be the simplest, but
    it was fun to play with coroutines as generators:


    set info1 {SYNC 1;OP1 4;OP2 4;SYNC 2;OP3 3;SYNC 3;SYNC 4}
    set info2 {SYNC 1;SYNC 2;SYNC 3;OP2 4;OP3 4;SYNC 4}

    proc generator {info} {
    foreach n [split $info {;}] {yield $n}
    return
    }

    set result1 {}
    set result2 {}

    # Start a generator with each of the input strings
    set str1 [coroutine gen1 generator $info1]
    set str2 [coroutine gen2 generator $info2]

    # An empty string indicates the generator is finished
    while {$str1 ne "" || $str2 ne ""} {
    # Check which input is at a SYNC item, if any
    set sync1 [expr {$str1 eq "" || [string match {SYNC *} $str1]}]
    set sync2 [expr {$str2 eq "" || [string match {SYNC *} $str2]}]
    # Initialize the output variables
    set val1 $str1
    set val2 $str2
    # Boolean XOR
    if {$sync1 != $sync2} {
    # Exactly one of the inputs is at a SYNC item
    if {$sync1} {
    # Determine an alternative value to use for output variable 1
    scan $str2 {%*s %d} num
    set val1 "NOOP [expr {7 - $num}]"
    } else {
    # Determine an alternative value to use for output variable 2
    scan $str1 {%*s %d} num
    set val2 "NOOP [expr {7 - $num}]"
    }
    }
    # Add the output variables to their respective lists
    lappend result1 $val1
    lappend result2 $val2
    # Take the next value from the input that is not at a SYNC item.
    # If they are both at a SYNC item, take the next value from both
    inputs.
    if {!$sync1 || $sync2} {set str1 [gen1]}
    if {!$sync2 || $sync1} {set str2 [gen2]}
    }

    # Present the result:
    puts "result1: [join $result1 {;}]"
    puts "result2: [join $result2 {;}]"


    Schelte

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)