The [format] command seems like a nicer, possibly more cleaner-looking way of doing it. Should I be using that more, or is it mostly personal preference?
"Here is some ${output} saved to a ${test}.txt file"
vs
[format "Here is some %s saved to a %s.txt file" $output $test]
Thanks!
Another option is to use message catalog (i.e. the msgcat package that come
with Tcl).
--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+
Unless you need formatting support (for example, to make sure numbers
are formatted properly or similar lines are aligned vertically), puts is
fine.
DrS
Actually, in your first example, there's no need for the braced variable
names either. You'll only need to resort to bracing the names if the
characters immediately following the referenced variable could be
interpreted as part of the variable name.
So, for instance, you can't do this:
set foo "abc"
"Here is $foobarbaz"
... because it's not possible to determine where the variable name ends
and the literal text begins. In that case, you have to do something
like you've specified above.
However, where there is no ambiguity involved, there is no need to brace
the variable. Assuming sane variable names in your example (output and
test), there is no need for the bracing. However, if you intended to
access a variable named "test." above, proper bracing would be necessary
to avoid ambiguity.
That said, it certainly doesn't hurt anything to add the braces, even
when unnecessary.
Jeff
>
> On 4/1/2011 2:01 PM, scottdware wrote:
> > So, I've got a script that writes output to config files, etc. Now, I normally use the ${variable} method when I'm within double-quotes and need to print out a variable.
> >
> > The [format] command seems like a nicer, possibly more cleaner-looking way of doing it. Should I be using that more, or is it mostly personal preference?
If/When you opt for internationisation, you will need to use the format
command:
package require msgcat
proc _ {args} {return [eval ::msgcat::mc $args]}
puts [format [_ "Here is some %s saved to a %s.txt file"] $output $test]
> >
> > "Here is some ${output} saved to a ${test}.txt file"
> >
> > vs
> >
> > [format "Here is some %s saved to a %s.txt file" $output $test]
>
> Actually, in your first example, there's no need for the braced variable
> names either. You'll only need to resort to bracing the names if the
> characters immediately following the referenced variable could be
> interpreted as part of the variable name.
>
> So, for instance, you can't do this:
>
> set foo "abc"
>
> "Here is $foobarbaz"
>
> ... because it's not possible to determine where the variable name ends
> and the literal text begins. In that case, you have to do something
> like you've specified above.
>
> However, where there is no ambiguity involved, there is no need to brace
> the variable. Assuming sane variable names in your example (output and
> test), there is no need for the bracing. However, if you intended to
> access a variable named "test." above, proper bracing would be necessary
> to avoid ambiguity.
>
> That said, it certainly doesn't hurt anything to add the braces, even
> when unnecessary.
>
> Jeff
>
>
--
Robert Heller -- 978-544-6933 / hel...@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
Note however that [msgcat::mc] already does the format for you; just
pass in those parameters as additional arguments like this:
puts [_ "Here is some %s saved to a %s.txt file" $output $test]
Easy! Shorter! What's not to like?
Donal.
With Tcl 8.5+, you'd want
proc _ {args} {return [::msgcat::mc {*}$args]}
>
> puts [format [_ "Here is some %s saved to a %s.txt file"] $output $test]
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
> At 2011-04-01 04:54PM, "Robert Heller" wrote:
> > At Fri, 01 Apr 2011 15:00:28 -0500 Jeff Godfrey <jeff_g...@pobox.com> wrote:
> >
> > If/When you opt for internationisation, you will need to use the format
> > command:
> >
> > package require msgcat
> > proc _ {args} {return [eval ::msgcat::mc $args]}
>
> With Tcl 8.5+, you'd want
>
> proc _ {args} {return [::msgcat::mc {*}$args]}
Or, since that is the identity proc, use interp alias
to turn _ into ::msgcat::mc.
Another good benefit (of either approach) is that you
can easily change _ to be either mc or format, since the
args are the same.
--
Donald Arseneau as...@triumf.ca