Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trouble with string concatenation

48 views
Skip to first unread message

Luc

unread,
Mar 22, 2019, 9:40:07 PM3/22/19
to
I need to do this:

Get a file name: foo.so
and link it to a directory with a prefix:
file link -symbolic /directory/prefix_foo.so /original/directory/foo.so

My code:

file link -symbolic [join "$_directory/$_prefix _[file rootname [file tail $_dllfile]].so" ""] [file dirname $_dllfile]/[file rootname [file tail $_dllfile]].so

My problem: join really joins everything and I have problems with spaces in the file name:

"Another File.so" becomes "prefix_AnotherFile.so"

I want to preserve the spaces.

Help?

two...@gmail.com

unread,
Mar 22, 2019, 10:44:11 PM3/22/19
to
Your join statement has a "" for the separator. This will result in
the concatenation of the items in the list with no separator.

For example,

% join "a b c" ""
abc

whereas

% join "a b c" "/"
a/b/c

Take a look at [file join] as that's its job, to construct path names.

Rich

unread,
Mar 22, 2019, 10:53:54 PM3/22/19
to
Luc <luc...@gmail.com> wrote:
> I need to do this:
>
> Get a file name: foo.so
> and link it to a directory with a prefix:
> file link -symbolic /directory/prefix_foo.so /original/directory/foo.so
>
> My code:
>
> file link -symbolic [join "$_directory/$_prefix _[file rootname [file tail $_dllfile]].so" ""] [file dirname $_dllfile]/[file rootname [file tail $_dllfile]].so
>
> My problem: join really joins everything and I have problems with
> spaces in the file name:

'join' is for use on lists.

'file join' is for file paths.

> "Another File.so" becomes "prefix_AnotherFile.so"

Wrong 'join' command, incorrect usage of double quotes.

> I want to preserve the spaces.

Then let 'file join' do the work, and don't try to 'overthink' things:

$ rlwrap tclsh
% set _directory directory
directory
% set _prefix prefix
prefix
% set _dllfile "Another File.so"
Another File.so
% file join $_directory ${_prefix}_[file rootname [file tail $_dllfile]].so
directory/prefix_Another File.so
%

Note that there are no double quotes in the example above. Each
parameter to 'file join' becomes one component of the final full file
path. The last parameter becomes the "filename part". Unless you are
inserting the spaces in the Tcl source being executed, there is no need
for the double quotes.

Luc

unread,
Mar 23, 2019, 4:33:01 AM3/23/19
to
[file join] is a completely wrong answer. It would concatenate my prefix and file name with a forward slash, creating a directory where it's not wanted.

Thanks to Rich. I had completely forgotten I could enclose a variable name with {} which rids me of using any kind of join altogether. Everything works fine now. Thank you.

Rich

unread,
Mar 23, 2019, 10:36:08 AM3/23/19
to
Luc <luc...@gmail.com> wrote:
> [file join] is a completely wrong answer. It would concatenate my
> prefix and file name with a forward slash, creating a directory where
> it's not wanted.

No. 'file join' is the correct answer. But if you want to create, in
Tcl, a filename with a space, you need to make sure to enclose that
string in double quotes, because otherwise 'file join' sees two
parameters, and puts in a slash between the parameters.

I.e., you need the entire filename you want to use to be the *last*
parameter to 'file join'.

Your response above says you were doing this:

% file join dir1 dir2 dir3 name_prefix name_suffix
dir1/dir2/dir3/name_prefix/name_suffix

when you should have done this:

% file join dir1 dir2 dir3 "name_prefix name_suffix"
dir1/dir2/dir3/name_prefix name_suffix

Note the *significant* difference. The first has a last parameter of
just "name_suffix", the second has a last parameter of
"name_prefix name_suffix". Understanding the importance of this
distinction is critical.

0 new messages