Endloop condition

I’m trying to do a endloop condition but It doesn’t really seem to work the way I intended.

I have the following peace of code which should end at i = 5, but ends in the very first iteration. What am I doing wrong?

+prog template urs:66.1
head Endloop condition
    TXB      a  b
    loop#i 10
        let#a 10-#i
        let#b #i
        TXB i=#i: #a #b
    endloop #a == #b
    $endloop
END

Hello,

let’s have a look at the manual, 8.12.17 “LOOP, ENDLOOP”
“It may also be terminated if theexpression following ENDLOOP becomes zero or negative.”

+prog template urs:66.1
 head Endloop condition
      TXB      a  b
       loop#i 10
          let#a 10-#i
          let#b #i
          TXB i=#i: #a #b
       endloop (#a-#b)   $ <---------
     $  It may also be terminated if the expression following ENDLOOP becomes zero or negative.
 END

Greetings from NRW
Philipp

1 Like

This is very promising!

I have now expanded the example a bit, but unfortunately I’m not able to transfer this condition.

In this example I have a list of X-coordinates and a list of nodenumbers (x_unsorted1 and node_unsorted1).
Furthermore I have a list of sorted X-coordinates (X-sorted1). My example runs through both lists and finds the indices in the sorted list corresponding to the unsorted list. I then use this index number to sort a new list (node_sorted1) containing node numbers sorted by X-coordinates.

+prog template urs:66.1
    head Endloop condition

    let#x_unsorted1      1,     5,  3,  9,  8,  7,  4,  6,  2
    let#node_unsorted1   11,    19, 16, 15, 14, 18, 12, 13, 17

    let#x_sorted1        1,     2,  3,  4,  5,  6,  7,  8,  9
    ! output should be: 11      17  16  12  19  13  18  14  15

    loop#i x_Sorted1
        loop#j x_Unsorted1
            if #x_Sorted1(#i) == #X_Unsorted1(#j)
                let#X_id1(#i) #j
            endif
            prt#j
        endloop !(#x_Sorted1(#i)-#X_Unsorted1(#j))
        sto#node_sorted1(#i) #node_unsorted1(#X_id1(#i))
    endloop

    loop#i node_sorted1
        let#prt #node_sorted1(#i)
        prt#prt
    endloop
end

OUTPUT:

  25         ENDLOOP
---- CADINT VARIABLE PRT             (     0) =       11.00
---- CADINT VARIABLE PRT             (     0) =       17.00
---- CADINT VARIABLE PRT             (     0) =       16.00
---- CADINT VARIABLE PRT             (     0) =       12.00
---- CADINT VARIABLE PRT             (     0) =       19.00
---- CADINT VARIABLE PRT             (     0) =       13.00
---- CADINT VARIABLE PRT             (     0) =       18.00
---- CADINT VARIABLE PRT             (     0) =       14.00
---- CADINT VARIABLE PRT             (     0) =       15.00
  26     END   

So the function works as intended, but as it is now it runs through loop#j way too many times, and could be ended as soon as (#x_Sorted1(#i)-#X_Unsorted1(#j)) = 0 is met, but again it doesn’t seem to work as intended.
So how do I make this endloop condition work?

Hello MRCH,

I wrote a tiny workaround:

loop#i x_Sorted1
    let#BREAK 2
    loop#j x_Unsorted1
        if #x_Sorted1(#i) == #X_Unsorted1(#j)
            let#X_id1(#i) #j
            let#BREAK -1
        endif
        prt#j
    endloop #BREAK     $  (#x_Sorted1(#i)-#X_Unsorted1(#j))
    sto#node_sorted1(#i) #node_unsorted1(#X_id1(#i))
endloop

Greetings from NRW
Philipp

1 Like

Beautiful!

Just for my own understanding of the limitations… is the reason my initial code didn’t work that the endloop condition can’t handle two variable? Or that it can’t be an indexed list?

I fail to see the exact difference between the break workaround and what I already did.

Nevertheless, it works perfectly. Thank you very much!