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

incr and single characters ...

60 views
Skip to first unread message

MartinLemburg@UGS

unread,
Oct 9, 2006, 6:00:55 AM10/9/06
to
Hello,

working in both worlds of C++ and tcl I sometimes miss the C++
"feature" of the mixture of (unsigned) char and integer letting me
increment or decrement even characters.

In tcl the incr command only allows to increment pure integer
values/strings.

What's about extending the behaviour of incr to allow to "increment"
characters too?

So today I would do:

% set char "a"
a
% set ascii [scan $char {%c}]
97
% incr ascii
98
% set char [format {%c} $ascii]
b

With the behaviour change I would simply do:

% set char "a"
a
% incr char
b

Perhabs for some people it's only playing around, but it would easier
such things a lot.

Best regards,

Martin Lemburg
UGS - Transforming the Process of Innovation

Alan Anderson

unread,
Oct 9, 2006, 6:50:18 AM10/9/06
to
"MartinLemburg@UGS" <martin.le...@gmx.net> wrote:

> working in both worlds of C++ and tcl I sometimes miss the C++
> "feature" of the mixture of (unsigned) char and integer letting me
> increment or decrement even characters.

That works because "characters" in C are just 8-bit integers. In C,
*nothing* is a string. :-P

> In tcl the incr command only allows to increment pure integer
> values/strings.
>
> What's about extending the behaviour of incr to allow to "increment"
> characters too?

> [...]


> With the behaviour change I would simply do:
>
> % set char "a"
> a
> % incr char
> b

What would you want it to do if you started with "9"?

MartinLemburg@UGS

unread,
Oct 9, 2006, 7:02:01 AM10/9/06
to
Hi Alan,

I know, that every thing in tcl is a string, but under certain
conditions a string can be handled as if it is of a special type.
This is done with integers, doubles, boolean, etc..

Why not introducing in the tcl command incr the following behaviour:

Is the variable content an integer value?
Yes:
Remember the type of incrementation
No:
Is it the string representation one character long?
Yes:
Get the integer value of the character
Remember the type of incrementation
No:
raise an error reasoning the invalid type
Increment the integer value
Was the incrementation an character manipulation?
Yes:
convert the integer value into an one-character-string
return the one-character-string
No:
return the incremented integer value

Since tcl allows strings to have "types", it would be no harm to the
rule "everything is a string" to care for one-character-strings like
caring for integers, wouldn't it?
And why not caring for one-character-strings like caring for integers
in all of the mathmatics?

Best regards,

Martin Lemburg
UGS - Transforming the Process of Innovation

On Oct 9, 12:50 pm, Alan Anderson <arand...@insightbb.com> wrote:


> "MartinLemburg@UGS" <martin.lemburg....@gmx.net> wrote:
> > working in both worlds of C++ and tcl I sometimes miss the C++
> > "feature" of the mixture of (unsigned) char and integer letting me

> > increment or decrement even characters.That works because "characters" in C are just 8-bit integers. In C,


> *nothing* is a string. :-P
>
> > In tcl the incr command only allows to increment pure integer
> > values/strings.
>
> > What's about extending the behaviour of incr to allow to "increment"
> > characters too?
> > [...]
> > With the behaviour change I would simply do:
>
> > % set char "a"
> > a
> > % incr char

> > bWhat would you want it to do if you started with "9"?

Bryan Oakley

unread,
Oct 9, 2006, 7:04:22 AM10/9/06
to
MartinLemburg@UGS wrote:
> Hello,
>
> working in both worlds of C++ and tcl I sometimes miss the C++
> "feature" of the mixture of (unsigned) char and integer letting me
> increment or decrement even characters.
>
> In tcl the incr command only allows to increment pure integer
> values/strings.
>
> What's about extending the behaviour of incr to allow to "increment"
> characters too?
>

I think the fact that you may be the only person to have asked for this
feature in the last decade gives us a hint about the utility and
priority of such a change :-)


--
Bryan Oakley
http://www.tclscripting.com

Donald Arseneau

unread,
Oct 9, 2006, 7:16:04 AM10/9/06
to
"MartinLemburg@UGS" <martin.le...@gmx.net> writes:

> working in both worlds of C++ and tcl I sometimes miss the C++
> "feature" of the mixture of (unsigned) char and integer letting me
> increment or decrement even characters.

Why the C behaviour? Wouldn't you like the perl version of
"smart" incrementing?


--
Donald Arseneau as...@triumf.ca

Fredderic

unread,
Oct 9, 2006, 7:19:16 AM10/9/06
to
On Mon, 09 Oct 2006 11:04:22 GMT,
Bryan Oakley <oak...@bardo.clearlight.com> wrote:

> > What's about extending the behaviour of incr to allow to "increment"
> > characters too?
> I think the fact that you may be the only person to have asked for
> this feature in the last decade gives us a hint about the utility and
> priority of such a change :-)

Well, that may be a little harsh...

I know of at least two instances in the past decade when I'd have
appreciated that ability. In one of those cases, it would have caused
more harm than good, but that's beside the point. :)


Fredderic

MartinLemburg@UGS

unread,
Oct 9, 2006, 7:22:19 AM10/9/06
to
Hi Bryan,

being the only one asking for a feature, doesn't mean it isn't a good
one.

Ok - for me it is not an everyday feature, but there were several
moments in the last "decade" were would have loved to have such a incr
tcl command.

Best regards,

Martin Lemburg
UGS - Transforming the Process of Innovation

On Oct 9, 1:04 pm, Bryan Oakley <oak...@bardo.clearlight.com> wrote:
> MartinLemburg@UGS wrote:
> > Hello,
>
> > working in both worlds of C++ and tcl I sometimes miss the C++
> > "feature" of the mixture of (unsigned) char and integer letting me
> > increment or decrement even characters.
>
> > In tcl the incr command only allows to increment pure integer
> > values/strings.
>
> > What's about extending the behaviour of incr to allow to "increment"

> > characters too?I think the fact that you may be the only person to have asked for this

MartinLemburg@UGS

unread,
Oct 9, 2006, 7:23:23 AM10/9/06
to
Hi Donald,

sorry to say, but I don't know anything about perl and so the "smart"
incrementing is something you should explain to me!

Best regards,

Martin Lemburg
UGS - Transforming the Process of Innovation

On Oct 9, 1:16 pm, Donald Arseneau <a...@triumf.ca> wrote:


> "MartinLemburg@UGS" <martin.lemburg....@gmx.net> writes:
> > working in both worlds of C++ and tcl I sometimes miss the C++
> > "feature" of the mixture of (unsigned) char and integer letting me

> > increment or decrement even characters.Why the C behaviour? Wouldn't you like the perl version of
> "smart" incrementing?
>
> --
> Donald Arseneau a...@triumf.ca

Ralf Fassel

unread,
Oct 9, 2006, 7:51:18 AM10/9/06
to
* "MartinLemburg@UGS" <martin.le...@gmx.net>

| sorry to say, but I don't know anything about perl and so the
| "smart" incrementing is something you should explain to me!

Snip from perl docs:

The autoincrement operator has a little extra built-in magic to
it. If you increment a variable that is numeric, or that has ever
been used in a numeric context, you get a normal increment. If,
however, the variable has only been used in string contexts since
it was set, and has a value that is not null and matches the
pattern `/^[a-zA-Z]*[0-9]*$/', the increment is done as a string,
preserving each character within its range, with carry:

print ++($foo = '99'); # prints '100'
print ++($foo = 'a0'); # prints 'a1'
print ++($foo = 'Az'); # prints 'Ba'
print ++($foo = 'zz'); # prints 'aaa'

R'

Uwe Klein

unread,
Oct 9, 2006, 8:03:33 AM10/9/06
to

set act hal
set next [ string increment $act ]
puts "$next"
ibm


uwe

Don Porter

unread,
Oct 9, 2006, 9:50:58 AM10/9/06
to
MartinLemburg@UGS wrote:
> being the only one asking for a feature, doesn't mean it isn't a good
> one.

True, but it does suggest this feature might be best provided by your
own command rather than surprising mods to a base command. The proc
should be quite simple.

DGP

Cameron Laird

unread,
Oct 9, 2006, 9:51:36 AM10/9/06
to
In article <ygaejth...@panther.akutech-local.de>,

That's a passage to savor, certainly worth quoting with the
care you did. 'Makes me want to fight against a TIP or two
... Anyway, I think it's a *good* thing that different
languages are different--and this nicely illustrates how
great those differences in character can sometimes be.

suchenwi

unread,
Oct 9, 2006, 10:21:55 AM10/9/06
to

Don Porter schrieb:

> The proc should be quite simple.

% proc cincr {c {amount 1}} {scan $c %c i; format %c [incr i $amount]}
% cincr A
B
Except for much-needed features, coding it oneself is always better
than TIPping :)

Gerhard Reithofer

unread,
Oct 9, 2006, 5:13:23 PM10/9/06
to
On Mon, 9 Oct 2006, MartinLemburg@UGS wrote:

> Hello,
>
> working in both worlds of C++ and tcl I sometimes miss the C++

...

> What's about extending the behaviour of incr to allow to "increment"
> characters too?
>
> So today I would do:
>
> % set char "a"
> a
> % set ascii [scan $char {%c}]
> 97
> % incr ascii
> 98
> % set char [format {%c} $ascii]
> b

...

rename incr _incr
proc incr {c} {
set a [scan $c %c]
format %c [_incr a]
}

% incr a
b
% incr 9
:

It worx ;-)

Bye,
Gerhard Reithofer
Tech-EDV Support Forum - http://www.tech-edv.co.at/vcms/

0 new messages