javajvmtcljacl

TCL String Manipulation with curly braces


I'm modifying an application backout script in JACL. This script is designed to search for a JVM argument string that we want to remove within the JVM arguments of the server. New to the application this release cycle is a jvm argument ${variable_name}. My old code

set ixReplace [lsearch -exact $jvm_args "string_to_search"]
set jvm_args [lreplace $jvm_args $ixReplace $ixReplace]

now returns an extra set of {} like this

-Xjit:disableOSR -Xgc:preferredHeapBase=0x100000000 -Xmnx1152m -Xmns512m
-Xgcpolicy:gencon -agentlib:getClasses -noverify {${variable_name}}

I've found multiple articles about how to remove the extra {} here and here but I cannot seem to set the results to a variable to which i'm using to set new jvm arguments.

My ultimate goal is to have the correct string set to a variable called jvm_args so that I may use to update the JVM arguments like this.

set attr {}
lappend attr [list genericJvmArguments  $jvm_args]
$AdminConfig modify $server_jvm_id $attr

Any help or suggestions would be greatly appreciated.


Solution

  • Tcl's adding those braces because you've got a well-formed Tcl list after the lreplace operation, and not any old string. The braces stop the ${variable_name} from ever being interpreted as a variable substitution; the $ is a Tcl metasyntax character. (Square brackets would also attract quoting, as would a few other characters too.)

    However, you're wanting to feed the result into a context that doesn't expect a Tcl list, but rather probably a simple space-separated string. The simplest approach is to just use join at the point where you stop thinking of having a Tcl list of words and start thinking of having a general string, probably like this:

    lappend attr [list genericJvmArguments [join $jvm_args]]
    

    It won't cope well if you've got spaces embedded in the string, or a few other cases, but without knowing the exact criteria for what makes a word a word in the source material or how to quote things in the system that is taking them, this is the best you're likely to get. (It is at least cheap to do this much…)