Trouble using variables as the basis of an Iterate.

Clovertech Forums Cloverleaf Trouble using variables as the basis of an Iterate.

  • Creator
    Topic
  • #118892
    David Burgess
    Participant

      I’ve build a very basic translation to try and trouble shoot an issue I’m have where I want to use a variable “@listBasis” as the basis for an iteration.  I found a good suggestion in a previous post about how you could pass the list and the pointer value ($%l1) as input to the copy statement and then use those values to pull an individual value out of the list and place into an instance of your output field.  I appear to be having two issues.  The first is that the iterate does not appear to execute for each item in the variable list, and the second is that the value of “$%l1” in my copy statement is returning the name of the variable, and not the index counter.  I’ve attached screen shoots below that show my entire setup.  Here are the results from the testing tool when attempting to run the translation.  NOTE: The first line of the output below was generated from the echo statement in my copy statement to show what was found in xlateInVals.

      myList: one two three four five six — pointer @listBasis
      MESSAGE 1
      Tcl callout error
      set myList [lindex $xlateInVals 0]
      set pointer [lindex $xlateInVals 1]
      echo “myList: $myList — pointer $pointer”
      set xlateOutVals

        ]:

        errorCode: TCL VALUE INDEX

        errorInfo:
        bad index “@listBasis”: must be integer?[+-]integer? or end?[+-]integer?
        while executing
        “lindex $myList $pointer”
        invoked from within
        “list [lindex $myList $pointer]”
        invoked from within
        “set xlateOutVals

          ]”
          <End of errorInfo>

           

          Any input or suggestions would be greatly appreciate.

          Thanks,

          David

          Attachments:
          You must be logged in to view attached files.
        Viewing 14 reply threads
        • Author
          Replies
          • #118895
            Paul Bishop
            Participant

              Hi David!

              I believe %l1 has the value in the list (“one”, “two”, “three”, so forth) , not an index so that is why it is bombing on the lindex command.

              Since we aren’t on a version that does that yet (getting there!), I usually use the work-around that Jim K has in this post: Iterate using the list option – Clovertech (infor.com)

              Paul Bishop
              Carle Foundation Hospital
              Urbana, IL

            • #118896
              Keith McLeod
              Participant

                Not sure I ever got that method to work.  I had to make use of

                putting the list elements into the basis

                Basis:  one two three four five six

                Variable: %l1

                And the iterate would step through the values contained in the basis.  Never was successful providing a variable containing a list.  I have seen tricks of using

                0 1 as your basis and resetting the value of your iterate back to ‘0’ if you had more elements in your at list basis and updating a counter variable to use as the index of the elements within your list.  Use length of your list to compare against your counter variable…

                 

                 

              • #118897
                Ted Viens
                Participant

                  It took a little work, but I did get this to work.

                  I used an iteration of type “user” with the basis of @listBasis and a variable of %u1.  This is where @listBasis was initialized using “=one two three four five six” as found in your example.  I did define a separate counter variable @counter and incremented it while in the loop.

                  I have attached screen shots for you

                   

                  Input: =one two three four five six

                  Output:

                  @listBasis – {one two three four five six}
                  *** Inside List Iteration ***
                  List Item: one : List Counter: 1
                  *** Inside List Iteration ***
                  List Item: two : List Counter: 2
                  *** Inside List Iteration ***
                  List Item: three : List Counter: 3
                  *** Inside List Iteration ***
                  List Item: four : List Counter: 4
                  *** Inside List Iteration ***
                  List Item: five : List Counter: 5
                  *** Inside List Iteration ***
                  List Item: six : List Counter: 6

                   

                  I attached the translation in a text file.  I also attached images.  I hope this is what you were looking for, but at least you will be able to iterate the list and build upon that.

                   

                  • This reply was modified 3 years, 7 months ago by Ted Viens.
                  • This reply was modified 3 years, 7 months ago by Ted Viens.
                  Attachments:
                  You must be logged in to view attached files.
                • #118904
                  Ted Viens
                  Participant

                    If you need to locate an item inside of the @listBasis variable that can be done easily.  Since this is a simple list the lsearch command will provide you the location first instance of a value by default.

                  • #118905
                    Jim Kosloskey
                    Participant

                      Yes User is new however what I have seen is the ITERATE Variable (%l1 perhaps) contains the current repetition value in the list NOT the index position in the list. So Ted Vein’s use of a variable as a counter you need to maintain gets you the value the index would be.

                       

                      email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

                    • #118906
                      David Burgess
                      Participant

                        Thank you all!  Good stuff, got me what I needed.  (Hi Paul!!  Hope life is treating you well!)

                        I had to make a slight adjustment as I wanted to manipulate the output iteration variable, but after seeing what Ted had done it was an easy adjustment.  (I initialized @counter to zero, and added a line in the xlate to move the value of @counter to $%l1 which is what I used as the output iteration value.)  If you are interested, I’ve attached the updated version of the Xlate.  I didn’t even need to add pre-proc code at that point.  Here is my output form the testing tool:

                        MESSAGE 1
                        Warnings(0).warningCode >one<
                        Warnings(1).warningCode >two<
                        Warnings(2).warningCode >three<
                        Warnings(3).warningCode >four<
                        Warnings(4).warningCode >five<
                        Warnings(5).warningCode >six<

                        Thanks again, I would have figured this out your help!!

                        David

                         

                        Attachments:
                        You must be logged in to view attached files.
                      • #118908
                        Ted Viens
                        Participant

                          Thank you for sharing your final solution.  I was happy to help.

                        • #118929
                          Robert Kersemakers
                          Participant

                            Cool!
                            Is this a new CL19 feature where we can finally iterate over a dynamic list? So no more iterating over %l9 with basis {0 1} and setting $%l9 to 0 if we want the iteration to continue?

                            Ah: a new type ‘user’. Must fiddle around with that, as soon as we are done migrating from 6.2 to 19.1. Looks good though!

                            Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

                          • #118931
                            Jeff Dawson
                            Participant

                              Curious if your list element isn’t one word say it contained two words with a space.  I’ve used the List iterate option described above to build a header or footers containing address information.   This user iterate looks pretty nice but I’m stuck on how you can pass an item in the list that contains spaces.  From what I can tell the user iterate treats a space regardless if it’s own element individually.

                              In the past when checking this type of condition within an Xlate for an IF statement I’d create a @variable to honor the spaces so it would work

                              Source: =   This has Spaces  Destination: @variableWithSpace

                              In the example  of  =one two three four five six {seven with spaces} I tried adding another element with spaces but it treated  {seven with spaces} as individual list elements which wasn’t the desired result.

                              I tried a few different things but didn’t’ have any luck passing a list element with spaces in my temp variable used as the basis in the user iterate.  I ended up creating a lookup table that could map the list element name to an output value that has spaces.  For example in the lookup table:

                              @hospName – Hospital name

                              @hospAddress – Street, city, zip code

                            • #118932
                              Jim Kosloskey
                              Participant

                                Have you tried escaping the curly braces?

                                =one two three four five six \{seven with spaces\}

                                Perhaps the curly braces are being stripped off in the COPY.

                                email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

                              • #118933
                                Jeff Dawson
                                Participant

                                  Hi Jim,

                                  I’ve tried a few different approaches but for some reason it looks like the user iterate regardless of what is set as list element just still just looks at the space and treats it as a individual list element when going through the iterate.

                                  Copy Source: =one two three \{seven with spaces\}

                                  @listBasis – {one two three \{seven with spaces\}}

                                  *** Inside List Iteration ***
                                  List Item: one : List Counter: 1
                                  List Item: two : List Counter: 2
                                  List Item: three : List Counter: 3
                                  List Item: \{seven : List Counter: 4
                                  List Item: with : List Counter: 5
                                  List Item: spaces\} : List Counter: 6

                                • #118934
                                  Jeff Dawson
                                  Participant

                                    I think your right in regards to the use of the Copy function or even Concat where the braces for whatever reason are not being honored.  I manually updated the basis of the user iterate from using the temp variable to just placing the list items and it worked.  Still playing around with different options to see if it’s possible to get the temp variable working.

                                    one two three {seven with spaces}

                                    ** Inside List Iteration ***
                                    List Item: one : List Counter: 1
                                    List Item: two : List Counter: 2
                                    List Item: three : List Counter: 3
                                    List Item: seven with spaces : List Counter: 4

                                     

                                     

                                  • #118935
                                    David Burgess
                                    Participant

                                      I ran into the same issue.  I had to take my list and used a COPY statement with source @myList –> @myIterationCnt and the used the following code in the pre-proc:
                                      <pre>set dummyList [split [lindex $$xlateInVals 0] ]
                                      set dummyList [llength $dummyList]
                                      set output “”
                                      for {set i 0} {$i < $dummyList} {incr i} {lappend output $i}
                                      set xlateOutVals

                                        </pre>
                                        After this my variable @myIterationCnt would contain 5 entries {0 1 2 3 4} if my list had five entries.  I could then use @myIterationCnt as the variable in the iterate.  This solved the iterate issue, but you then have to pull out the actual value yoyu want from your list.  To do that, I have a COPY statement inside the iterate, that has two values on the input.  $%u1  and @myList.  The target of the copy is my output field, but I have the following code as a pre-proc in the COPY statement:
                                        <pre>set index [lindex $xlateInVals 0]
                                        set drugList [lindex $xlateInVals 1]
                                        set drugList [split $drugList ]
                                        set returnValue [lindex $drugList $index]
                                        set xlateOutVals

                                          </pre>
                                          This works because both code blocks in the pre-proc knows to respect the curly braces, while the iterate does not.  Hope this helps, sorry I didn’t update the thread when I ran across the issue myself.  If you are confused I can try to grab some screen shoots of my setup tonight or tomorrow.

                                          • #118941
                                            Jeff Dawson
                                            Participant

                                              Thanks David,

                                              That sounds like the same approach of how I was currently doing some thing like this…almost like a do while loop.  Get the total list element count loop then use a list iterate to loop through keeping a counter to pull out the list index based off that.  I hadn’t tried out this user iterate yet and saw this thread and was intrigued at how it already knew the index so it reduced a few lines of code.  Still trying to figure out how to pass in something with spaces but I did post a few options though may not be idea.

                                               

                                              Jeff

                                          • #118939
                                            Jeff Dawson
                                            Participant

                                              One other work around besides using a look up table that could work is using a character as a delimiter for a space, example below I used a tick and just string mapped it to the desired space.

                                              @listBasis – {one two three seven<code>with</code>spaces}

                                              echo “List Item: [string map {` ” “} [lindex $xlateInVals 0]] : List Counter: [lindex $xlateInVals 1]”

                                              *** Inside List Iteration ***
                                              List Item: one : List Counter: 1
                                              List Item: two : List Counter: 2
                                              List Item: three : List Counter: 3
                                              List Item: seven with spaces : List Counter: 4

                                              Also tried in the basis using a few temp variables for readability.

                                              @listBasis =one two three

                                              @seven = =seven <code>with</code>spaces

                                              basis: @listBasis @seven

                                              bah some of these characters are being stubborn when trying to post.

                                              • This reply was modified 3 years, 6 months ago by Jeff Dawson.
                                            • #118942
                                              David Burgess
                                              Participant

                                                Sorry appears I can’t edit my last post any more time.  (Formatting issues, so I must of maxed out my ability to edit that post.)  I too had issues with space delimited lists.  I did end up making my list pipe “|” delimited.  I’m attaching screenshots of my sample xlate to demonstrate how I finally got mine to work.  Hope it helps.

                                                 

                                                Attachments:
                                                You must be logged in to view attached files.
                                            Viewing 14 reply threads
                                            • You must be logged in to reply to this topic.