Switch Statement and variables

Clovertech Forums Read Only Archives Cloverleaf Tcl Library Switch Statement and variables

  • Creator
    Topic
  • #49412
    Nate Kruse
    Participant

      Ran into another problem.  Is there a way to use a switch statement and have it look at the value of a variable for the comparison?  In the following switch statement, the switch doesn’t recognize or match with $SegOne.  Even though I get SegmentType as MSH, it never hits the SegOne case.  I don’t want to hardcode MSH in the switch as that would defeat the purpose of using a variable.

      Any thoughts?  I’d appreciate it.

      Thanks,

      Nate.

      set SegOne MSH

      set SegmentType [lindex $seg 0]

      switch -exact — $SegmentType {

        $SegOne {

          puts stdout “Inside with $SegOne”

        }

        default {

          puts stdout “Outside with $SegmentType”

        }

      }

    Viewing 7 reply threads
    • Author
      Replies
      • #61860

        No. I don’t believe that’s possible. Here’s a demo based on your code.

        Code:

        set seg [list MSH EVN PID PV1 ORC OBR OBX]
        set SegOne MSH

        #set SegmentType [lindex $seg 0]
        set SegmentType {$SegOne}

        switch -exact — $SegmentType {
         $SegOne {
           puts stdout “Inside with $SegOne”
         }

         default {
           puts stdout “Outside with $SegmentType”
         }
        }

        The switch sees “$SegOne” as a literal string, not a variable name.

        -- Max Drown (Infor)

      • #61861

        What exactly are you trying to do? Maybe we can come up with an appropriate work around.

        -- Max Drown (Infor)

      • #61862
        Nate Kruse
        Participant

          I’m trying to create a tcl script that will be reusable in the future with minimal modifications.  I want to have a “bank” of variables at the top of the script.  The user can then open the script, change a few variables from the “bank” with fields they want to output (such as MSH or PID or anything) and then run the script against a file.  Not every person who runs the script will want the same field values extracted, so hardcoding values isn’t the way I want to go.

          I could go with If statements, but was hoping the switch would work.

        • #61863
          Steve Carter
          Participant

            Based on what you stated you wanted to do with the message, you could do this:

            set SegList “MSH PID”

            set SegmentType [lindex $seg 0]

            if { [lcontain $SegList $SegmentType] } {

          • #61864
            Nate Kruse
            Participant

              Steve Carter wrote:

              Based on what you stated you wanted to do with the message, you could do this:

              set SegList “MSH PID”

              set SegmentType [lindex $seg 0]

              if { [lcontain $SegList $SegmentType] } {

            • #61865
              Nate Kruse
              Participant

                Thanks for all the replies and your time.  I tried a few things, but this one by Graham Ellis at opentalk worked best.  I’m still able to use the switch statement.

                http://www.wellho.net/cgi-bin/opentalk/YaBB.pl?board=tcl;action=display;num=1184944258;start=0

              • #61866

                Could you post the final code block, please?

                -- Max Drown (Infor)

              • #61867
                Nate Kruse
                Participant

                  Max Drown wrote:

                  Could you post the final code block, please?

                  Sure, here is the switch part…

                  switch -exact — $SegmentType “

                    $SegOne {

                       puts stdout “Inside with $SegOne”

                    }

                    default {

                       puts stdout “Outside with $SegmentType”

                    }

              Viewing 7 reply threads
              • The forum ‘Tcl Library’ is closed to new topics and replies.