Dynamic variable name

Clovertech Forums Cloverleaf Dynamic variable name

  • Creator
    Topic
  • #116981
    David Teh
    Participant

       

      Hi folks,

       

      I’ve got a outbound TCL that iterating through a the ORC and OBR segments of an ORU message.

      Foreach ORC segments, I want to pick up the field 2 component 1. And be able to use them outside the loop.

      Managed to get that done but was hoping to improve on the naming of the variables used.

      Essentially, I have the following variables:

      ORC 1st segment, field 2, component 1 (variable FlagORC1)

      ORC 2nd segment, field 2, component 1 (variable FlagORC2)

      etc

       

      I was trying to get the variable names to look like:

      ORC1_fld2cpt1

      ORC2_fld2cpt1

      etc

      How would you add that last bit “_fld2cpt1” to the first part?

       

      TIA!

       

       

    Viewing 1 reply thread
    • Author
      Replies
      • #116984
        Charlie Bursell
        Participant

          Easy enough

          set base ORC1_

          set var fld2cpt1

          set $base$var “SOME VALUE”

          I will leave to you how to vary the variable part each time and also how to keep track of variable names used.

        • #116985
          Jeff Dinsmore
          Participant

            In a situation like this, I think it’s simpler to use an array.

            You’ll certainly do this in a loop.

            So, that would look something like this…

            # if there’s a possibility orcArray already exists – if testing in a shell, for example – you’ll want to unset it.

            —————

            catch {unset orcArray}

            # initialize an array element to keep track of how many pairs of values you extract

            set orcArray(numOrcSegments) 0
            set outOfOrcSegments 0
            while { 1 } {

            # get your ORC values here – if out of ORC segments, set the value in break out of while loop if you have no more ORC segments, set outOfOrcSegments to 1

            if { $outOfOrcSegments } {
            break
            } else {
            set i $orcArray(numOrcSegments)
            set orcArray(f1,c1,$i) $orc2_1
            set orcArray(f2,c1,$i) $orc3_1
            incr orcArray(numOrcSegments)
            }

            }

            —————

            When you want to access a value, precede the array name with $ – just like any other variable.
            This for loop will list all of the f1/f2 value pairs in the array.

            ———–

            for { set i 0 } { $i < $orcArray(numOrcSegments) } { incr i } {
            puts “index $i – f1 c1= $orcArray(f1,c1,$i), f2 c1= $orcArray(f2,c1,$i)”
            }

             

            <hr />

            You could also use parallel lists, keyed lists – always more than one way to skin the cat…

            Jeff Dinsmore
            Chesapeake Regional Healthcare

        Viewing 1 reply thread
        • You must be logged in to reply to this topic.