----------------------------------------------------------------------------------
@MSGID:
a0dd52c5
@REPLY:
<4f2cc5f8-80f3-4dee-878a-d2ef16d814aen@googlegroups.com> 7ce7c942
@REPLYADDR Schelte <nospam@wanadoo.nl>
@REPLYTO 2:5075/128 Schelte
@CHRS: CP866 2
@RFC: 1 0
@RFC-References:
<4f2cc5f8-80f3-4dee-878a-d2ef16d814aen@googlegroups.com>
@RFC-Message-ID:
@TZUTC: 0200
@PID: Mozilla Thunderbird
@TID: FIDOGATE-5.12-ge4e8b94
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
--- Mozilla Thunderbird
* Origin: KPN B.V. (2:5075/128)
SEEN-BY: 5001/100 5005/49 5015/255 5019/40 5020/715
848 1042 4441 12000
SEEN-BY: 5030/49 1081 5058/104 5075/128
@PATH: 5075/128 5020/1042 4441