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

"A string, or not a string", that is the question ;)

15 views
Skip to first unread message

Richard Owlett

unread,
Jul 12, 2007, 3:21:35 PM7/12/07
to
Quoting from reference on "scan" in _ActiveTcl User Guide_ by ActiveState:
It is read in and the value is stored in the variable as a
*decimal* string.
It is read in and stored in the variable as a
*floating-point* string.
(emphasis added)

What? Why?

I think what I'm asking for is a pointer to discussion of how a "thingy"
[be it data, object, variable, ???] is represented/stored/??? by Tcl.

Without Tcl complaining I did

set a "-123 -456"
scan $a "%s %d" x y
set c abs($x)
set d [expr $c -24.1]
puts $d

Any of the few languages I know would have balked in some manner for
doing math on a "string". How lost a newbie am I?

Thank you. Tcl has fascinating possibilities.


Bryan Oakley

unread,
Jul 12, 2007, 4:01:25 PM7/12/07
to
Richard Owlett wrote:
> Any of the few languages I know would have balked in some manner for
> doing math on a "string". How lost a newbie am I?
>
> Thank you. Tcl has fascinating possibilities.

Think of all the programming languages of the world as a forest. Tcl is
a clearing in the middle of the forest where the sun shines, a cool
breeze blows, and you can get a breath of fresh air.

You might be lost if you've not been to the clearing before, but it's
real easy to find your way around. Just don't expect as many things to
be laying on the ground waiting to trip you up.

Oh, and you can make your own trees.


--
Bryan "Shameless purveyor of goofy analogies" Oakley

Gerald W. Lester

unread,
Jul 12, 2007, 4:42:36 PM7/12/07
to

*Everything* in Tcl can be represented as a string. Some strings can be
used as integers, some reals, some list and some as dictionaries -- when a
command needs one of its arguments as a particular "type" it asks the guts
of Tcl to convert it to that "type" if Tcl can not then an exception is
raised, if it can then it does.

> Thank you. Tcl has fascinating possibilities.

Yes, it does. For me the number one "possibility" for me is that it does
not needless attempt to get in the way of me getting my job done.

--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

sleb...@gmail.com

unread,
Jul 12, 2007, 7:41:21 PM7/12/07
to
On Jul 13, 3:21 am, Richard Owlett <rowl...@atlascomm.net> wrote:
> Quoting from reference on "scan" in _ActiveTcl User Guide_ by ActiveState:
>
> I think what I'm asking for is a pointer to discussion of how a "thingy"
> [be it data, object, variable, ???] is represented/stored/??? by Tcl.
>

Internally, for the sake of being fast, Tcl stores objects in their
own "types" like integers, doubles, lists, dicts and strings. However,
it does so only when needed at the last minute. So everything starts
out as a string. In addition, all objects in tcl has a string
representation. Hence from the user/programmer's point of view Tcl is
typeless. Which is why we say "everything is a string".

> Without Tcl complaining I did
>
> set a "-123 -456"

It should be noted that "" in tcl does not "make" something a string.
It is a quoting mechanism for which tcl has two: "" and {}. If you
were to write:

set x -456

then $x at this point is still a string, not an integer yet.

> scan $a "%s %d" x y

For further illustration of how typeless tcl is, you could have
written the above as:

set x [lindex $a 0]

or

set x [regexp -inline -- {.*\s+.*} $a]

though in this case, [scan] looks like good style.

> Any of the few languages I know would have balked in some manner for
> doing math on a "string". How lost a newbie am I?

Tcl belongs to a group of languages referred to as "dynamically
typed". Other languages that won't "balk" at doing maths on strings
include Perl and PHP. Although I feel Tcl is far more typeless than
Perl since in Tcl lists and dicts are just (from the programmer's
perspective) specially formatted strings.

Richard Owlett

unread,
Jul 13, 2007, 10:00:38 AM7/13/07
to
sleb...@yahoo.com wrote:
> On Jul 13, 3:21 am, Richard Owlett <rowl...@atlascomm.net> wrote:
>
>>Quoting from reference on "scan" in _ActiveTcl User Guide_ by ActiveState:
>>
>>I think what I'm asking for is a pointer to discussion of how a "thingy"
>>[be it data, object, variable, ???] is represented/stored/??? by Tcl.
>>
>
>
> Internally, for the sake of being fast, Tcl stores objects in their
> own "types" like integers, doubles, lists, dicts and strings. However,
> it does so only when needed at the last minute. So everything starts
> out as a string. In addition, all objects in tcl has a string
> representation. Hence from the user/programmer's point of view Tcl is
> typeless. Which is why we say "everything is a string".
>
>
>>Without Tcl complaining I did
>>
>>set a "-123 -456"
>
>
> It should be noted that "" in tcl does not "make" something a string.
> It is a quoting mechanism for which tcl has two: "" and {}. If you
> were to write:
>
> set x -456
>
> then $x at this point is still a string, not an integer yet.

That hadn't been a problem.

>
>
>>scan $a "%s %d" x y
>
>
> For further illustration of how typeless tcl is, you could have
> written the above as:
>
> set x [lindex $a 0]
>
> or
>
> set x [regexp -inline -- {.*\s+.*} $a]
>
> though in this case, [scan] looks like good style.

I was assuming that the %s conversion specification would force a
certain representation as quotes would in BASIC.

>
>
>>Any of the few languages I know would have balked in some manner for
>>doing math on a "string". How lost a newbie am I?
>
>
> Tcl belongs to a group of languages referred to as "dynamically
> typed". Other languages that won't "balk" at doing maths on strings
> include Perl and PHP. Although I feel Tcl is far more typeless than
> Perl since in Tcl lists and dicts are just (from the programmer's
> perspective) specially formatted strings.
>

Thanks to all for enlightenment.

Cameron Laird

unread,
Jul 13, 2007, 3:29:30 PM7/13/07
to
In article <1184283681....@e9g2000prf.googlegroups.com>,
sleb...@yahoo.com <sleb...@gmail.com> wrote:
.
.

.
>> Any of the few languages I know would have balked in some manner for
>> doing math on a "string". How lost a newbie am I?
>
>Tcl belongs to a group of languages referred to as "dynamically
>typed". Other languages that won't "balk" at doing maths on strings
>include Perl and PHP. Although I feel Tcl is far more typeless than
>Perl since in Tcl lists and dicts are just (from the programmer's
>perspective) specially formatted strings.
>

I generally insist on a different perspective to this question:
in Tcl, unlike most other common languages, there is no "math"
at a syntactic level. Tcl's arithmetic "belongs" to [expr] (and
arguably [incr] and a few others), and is determined by that
(those) command(s). This is quite different from what Perl and
PHP do (and yet again different from how Python, for example,
evaluates "'123' + '456'").

At the same time, yes, none of them refuse to "do math on strings".

0 new messages