Switch Command using variable for regexp

Clovertech Forums Read Only Archives Cloverleaf General Switch Command using variable for regexp

  • Creator
    Topic
  • #48229
    Keith McLeod
    Participant

      I am trying to use the switch command with the regexp switches.  It works when I explicitly use the pattern for the case.  I would like to make each pattern a variable.  This is where I run into trouble.  It doesn’t work.  I read somewhere that the curly braces surrounding the body of the switch statement needs to be removed.  Not successful there either.  

      If the variable should match with style_1 it drops to default.  Any suggestions?

      Here is a sample:

      set style_1 {^[0-9]$}

      foreach phone $List {

      switch -regexp — $phone {

      $style_1 {

      echo “Style 1 found”

      }

      ^[0-9][0-9]$ {

      echo “Style 2 found”

      }

      ^[0-9][0-9][0-9]$ {

      echo “Style 3 found”

      }

      ^[0-9][0-9][0-9][0-9]$ {

      echo “Style 4 found”

      }

      ^[0-9][0-9][0-9][0-9][0-9]$ {

      echo “Style 5 found”

      }

      default               {

      echo “Style default found”

      }

      }

      }

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

          You can not evaluate a variable inside braces like this.  You have to use a different style for the switch.  Try this:

          switch -regexp — $phone

                $style_1 {echo “Style 1 found”}

               {^[0-9][0-9]$} {echo “Style 2 found”}

               {^[0-9][0-9][0-9]$} {echo “Style 3 found”}

               {^[0-9][0-9][0-9][0-9]$} { echo “Style 4 found”}

               {^[0-9][0-9][0-9][0-9][0-9]$} {echo “Style 5 found”}

               default   {echo “Style default found} 8)

        • #58082
          Keith McLeod
          Participant

            Thanks… I did figure it out.  I knew it had to do with that.  The spaces between each case, I believe was the other part as to why it did not work at first…..

        Viewing 1 reply thread
        • The forum ‘General’ is closed to new topics and replies.