1

I am working on a project for my company and can't figure out how to extract the value from a variable let's call it 'a' and use it in the file name.

So, if the value of a is 1234 then as an example I want the file name to be 1234_abc.pcm I thought that maybe I could use the dollar sign to read the value from 'a' but that doesn't work and every combination that I could think of using curly and square brackets as well as parentheses have fallen short of the finish line. Nothing has worked all day. I end up getting $a_abc.pcm as the file name. Any thoughts / ideas would be a great help!

Thanks in advance,

ButtahNBred

2
  • 2
    The problem is that a_abc is a valid variable name, so you have to tell Tcl where your variable stops and the rest of the string begins. (see the variable substitution rule) Jun 25, 2013 at 22:48
  • I understand that but when I place it into a full path with the file name (i.e. C:\0000\${a}_abc) the recorded file is $a_abc. It doesn't replace ${a} with the numerical value that user inputs...the value of a is dynamic. You can look at a as a serial number rather than just a number that remains constant. Jun 26, 2013 at 3:29

4 Answers 4

3

I bet you're doing this:

set filename "c:\0000\${a}_abc.pcm"

The 2nd backslash, which you think is a directory separator, is actually escaping the $, preventing variable substitution [*].

Choose one of these alternatives:

set filename "c:\\0000\\${a}_abc.pcm"
set filename "c:/0000/${a}_abc.pcm"
set filename [format {c:\0000\%s_abc.pcm} $a]
set filename [file join c: 0000 ${a}_abc.pcm]  ;# my preferred solution

Windows handles forward slashes perfectly well)

[*] it's actually worse than that: due to the \000 you're actually getting a NUL byte. I'm not conversant with Tcl's implementation of strings and what is does with NULs, but:

$ tclsh <<< 'set a 1234; puts "c:\0000\${a}_foo"' | od -c
0000000   c   :  \0   0   $   {   a   }   _   f   o   o  \n
0000015
3
  • Fortunately, after discussions with people at work we discovered that it was easiest to set the recording directory using this command {C:\0000\ } and then append the serial number so the directory and filename exist. The forward slashes did not work for us so I guess there was a problem with our software recognizing the forward slashes as directory and filename delimiters. Jun 26, 2013 at 15:11
  • I advise using forward slashes (/) in the script and then file nativename to get backslashes when feeding into another program via exec (or showing to a user, if you really want to do that). Jun 26, 2013 at 18:19
  • In terms of what happens with a NUL, it'll truncate the filename when Windows gets it (assuming you're doing open) and the effects will be “interesting” if you feed into exec. (There's a subtle interplay of a few things there.) I know that on Unix, it just truncates… Jun 26, 2013 at 18:23
2

It sounds like you are looking for something like this?

set a "1234"
set suffix "_abc.pcm"
set filename "${a}${suffix}"
2
  • 1
    Not strictly necessary to use the suffix var: set filename ${a}_abc.pcm works just as well and is perhaps a little less cluttered. Jun 25, 2013 at 22:49
  • Sorry, I didn't clarify that 'a' in this situation is not static. It is dynamic and changes based on each run through. You can look at a as a serial number rather than a constant value. Jun 26, 2013 at 3:29
0

As Glenn Jackman points out; your problems are probably caused by backslashes.

To avoid these problems you should use the file command to construct your filenames -

% set a 1234
1234
% set filename [file join c:/ 0000 ${a}_abc.pcm
c:/0000/1234_abc.pcm
% nativeFilename [file join nativename $filename]
c:\0000\1234_abc.pcm

If you're going to use the filename within Tcl - e.g. in an open statement, you don't need to convert to native format with file nativename.

Using the file command is essential if you're writing code to be portable between Windows, Unix and Mac. Even if you aren't, it will help avoid backslash problems.

0

Fortunately, after discussions with people at work we discovered that it was easiest to set the recording directory using this command {C:\0000\ } and then append the serial number so the directory and filename exist.

The forward slashes did not work for us so I guess there was a problem with our software recognizing the forward slashes as directory and filename delimiters.

1
  • External tools probably need backslashes; that's what file nativename solves for you (and neatly!) Jun 26, 2013 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.