Set variable value inside switch statement Tcl

Lanzafame

Attempting to set two variables values according to the user input in a third variable. I am trying to use a switch statement but I am not sure if it is actually possible.

switch $sample_type_input {
  CORE { set sample_type 1 \
       set cutting ""}
  SWC  { set sample_type 2 \
       set cutting ""}
  CUTT { set sample_type 3 \
       set cutting "CUTT" }
}

I keep getting a 'wrong # args: should be "set varName ?newBalue?"` error.

Peter Lewerin

The backslash at the end of the line acts as a line continuation, concatenating this line and the next line into a single logical line of code. This is necessary in a case where one has to break up a command invocation into two or more lines, because of the parsing rules:

set foo
    bar

is interpreted as two command invocations: set foo and bar.

set foo \
    bar

is a single command invocation: set foo bar.

Note that the backslash must be the very last character before the newline: e.g. a space between the backslash and the newline simply makes the invocation end with a regular space and a quoted space:

# there is a space after the backslash here
set foo \ 
puts "{$foo}"
# => { }

If your command invocation includes an argument in braces, it's typically quite all right to break it up into several lines without a backslash, since all whitespace including newlines is quoted by the braces anyway:

set foo {
    bar
    baz
}

And indeed if the argument within braces is a script containing more than one command invocation, you need to break it up into lines (or insert semicolons), since newlines are significant as command line separators. Therefore this fails:

if {true} {set foo bar \
    set baz quz}

as it concatenates two command invocations into the single code line set foo bar set baz quz, providing the set command with five arguments when it expects one or two. The correct way is to write it as

if {true} {set foo bar ; set baz quz}

or

if {true} {set foo bar
    set baz quz}

letting the semicolon or newline separate the command invocations in the script argument. Stylistic convention is to add additional newlines at the beginning and end of a multi-line script argument, like this:

if {true} {
    set foo bar
    set baz quz
}

This is similar to C brace styles, but not quite the same thing. One common beginner mistake is to try to use some other C bracing convention such as

if {true}
{
    set foo bar
    set baz quz
}

which won't work since it introduces a codeline-separating newline between the first and second arguments to if. If you can't live without this bracing convention, you need to add a line continuation:

if {true} \
{
    set foo bar
    set baz quz
}

Documentation: if, puts, set, Summary of Tcl language syntax

(Note: the 'Hoodiecrow' mentioned in the comments is me, I used that nick earlier.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c++ Declaring a variable inside a switch statement

From Dev

Tcl variable substitution inside $::

From Dev

cannot change variable value inside if statement???? java

From Dev

Java: how to access a variable which was declared inside a switch statement

From Dev

Modifying empty string variable inside switch statement in function bash

From Dev

Inside a switch statement, is there any way to refer to the value being switched?

From Dev

Inside a switch statement, is there any way to refer to the value being switched?

From Dev

Unable to receive value from switch statement inside loop

From Dev

Variable will not be set when using SET /P inside IF statement

From Dev

is it possible to set a variable value using an if statement?

From Dev

is it possible to set a variable value using an if statement?

From Dev

variable scope in Tcl inside the procedure

From Dev

switch statement not changing a variable

From Dev

How to set the value of a liquid variable inside of Javascript

From Dev

tcl's subst seems slower than set, when getting a variable value from a variable with a variable's name

From Dev

Does a variable defined inside a switch/case persists it's value?

From Dev

Does a variable defined inside a switch/case persists it's value?

From Dev

Switch statement is returning no value

From Dev

MySQL IF inside a SWITCH CASE statement

From Dev

Switch statement inside Razor CSHTML

From Dev

PHP switch statement inside for loop

From Dev

PHP switch statement inside for loop

From Dev

MySQL IF inside a SWITCH CASE statement

From Dev

using switch statement inside a button

From Dev

Initialize variables inside a switch statement

From Dev

Switch Statement inside condition refactoring

From Dev

Use quotes for value inside set within a Update statement in SQL

From Dev

Using a variable value as a where statement inside PDO query

From Dev

Variable not working inside If statement

Related Related

  1. 1

    c++ Declaring a variable inside a switch statement

  2. 2

    Tcl variable substitution inside $::

  3. 3

    cannot change variable value inside if statement???? java

  4. 4

    Java: how to access a variable which was declared inside a switch statement

  5. 5

    Modifying empty string variable inside switch statement in function bash

  6. 6

    Inside a switch statement, is there any way to refer to the value being switched?

  7. 7

    Inside a switch statement, is there any way to refer to the value being switched?

  8. 8

    Unable to receive value from switch statement inside loop

  9. 9

    Variable will not be set when using SET /P inside IF statement

  10. 10

    is it possible to set a variable value using an if statement?

  11. 11

    is it possible to set a variable value using an if statement?

  12. 12

    variable scope in Tcl inside the procedure

  13. 13

    switch statement not changing a variable

  14. 14

    How to set the value of a liquid variable inside of Javascript

  15. 15

    tcl's subst seems slower than set, when getting a variable value from a variable with a variable's name

  16. 16

    Does a variable defined inside a switch/case persists it's value?

  17. 17

    Does a variable defined inside a switch/case persists it's value?

  18. 18

    Switch statement is returning no value

  19. 19

    MySQL IF inside a SWITCH CASE statement

  20. 20

    Switch statement inside Razor CSHTML

  21. 21

    PHP switch statement inside for loop

  22. 22

    PHP switch statement inside for loop

  23. 23

    MySQL IF inside a SWITCH CASE statement

  24. 24

    using switch statement inside a button

  25. 25

    Initialize variables inside a switch statement

  26. 26

    Switch Statement inside condition refactoring

  27. 27

    Use quotes for value inside set within a Update statement in SQL

  28. 28

    Using a variable value as a where statement inside PDO query

  29. 29

    Variable not working inside If statement

HotTag

Archive