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

Using [format] correctly at times

15 views
Skip to first unread message

scottdware

unread,
Apr 1, 2011, 3:01:23 PM4/1/11
to
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?

"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!

Gerald W. Lester

unread,
Apr 1, 2011, 3:29:57 PM4/1/11
to

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 |
+------------------------------------------------------------------------+

drsc...@gmail.com

unread,
Apr 1, 2011, 3:55:06 PM4/1/11
to
On 4/1/2011 3:01 PM, scottdware wrote:
> 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!


Unless you need formatting support (for example, to make sure numbers
are formatted properly or similar lines are aligned vertically), puts is
fine.


DrS

Jeff Godfrey

unread,
Apr 1, 2011, 4:00:28 PM4/1/11
to

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

unread,
Apr 1, 2011, 4:54:46 PM4/1/11
to
At Fri, 01 Apr 2011 15:00:28 -0500 Jeff Godfrey <jeff_g...@pobox.com> wrote:

>
> 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



scottdware

unread,
Apr 1, 2011, 5:53:06 PM4/1/11
to
All this was very helpful! Thanks a ton, guys!

Donal K. Fellows

unread,
Apr 2, 2011, 2:55:47 AM4/2/11
to
On Apr 1, 9:54 pm, Robert Heller <hel...@deepsoft.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]}
>
> puts [format [_ "Here is some %s saved to a %s.txt file"] $output $test]

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.

Glenn Jackman

unread,
Apr 2, 2011, 8:21:31 AM4/2/11
to
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]}

>
> 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

Donald Arseneau

unread,
Apr 2, 2011, 7:48:26 PM4/2/11
to
Glenn Jackman <gle...@ncf.ca> writes:

> 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

0 new messages