Reputation: 5469
I'm trying to append two string in tcl. I'm reading from csv and set values to the variables and then i will use that for assigning it my application. I tried the below one.
set vMyvalue [lindex $lsLine 17]
append vMyvalue " [lindex $lsLine 18]"
it is giving me the expected result. Like for e.g if i have values 250 and km in 17th and 18th position in csv. i'm getting
250 km
But the problem is when there are no values in the 17th and 18th i mean when it is empty, that time also it is adding space. But my application won't allow me to assign space for that value. How can i resolve this? I just started working in TCL. I'm not aware of many functions.
Upvotes: 0
Views: 2789
Reputation: 71598
I think the most intuitive way to handle cases similar to this one if you don't know a function to do this (including for example if you are joining two strings with some character but if any one of them are empty strings, then you want something different to be done), would be to use if
. In this case:
if {$vMyvalue eq " "} {set vMyvalue ""}
If you want to make your code somewhat shorter, you can make use of the functions lrange
(list range), join
and string
:
set vMyvalue [string trim [join [lrange $lsLine 17 18] " "]]
lrange
returns a list of elements from the list $lsLine
between indices 17 to 18 inclusive, then join
literally joins those elements with a space, and last, string trim
cleans up any leading and trailing spaces (removing the space completely if it is the only character in the string).
Upvotes: 1
Reputation: 114014
There are several ways to do this. The minimum modification from the code you already have is probably to trim the result. Trim removes leading and trailing whitespace but if it's only whitespace it would trim it to an empty string. So:
set myValue [string trim $myValue]
Upvotes: 1