I have a program that has a loop like:
while { [gets ...] != -1 } {
...
}
and it works fine. But there is no prompt before the "gets", so it is less than ideally user-friendly. I would like to add a prompt.
The obvious way to do this would be:
puts -nonewline "Enter something: "
while { [gets ...] != -1 } {
...
puts -nonewline "Enter something: "
}
But that is messy because you have to write the prompting command twice.
What you would like is something like:
while { puts -nonewline "Enter something: ", then do: [gets ...] != -1 } {
...
}
But I don't know what the TCL syntax for that is (the above is obviously pseudo-code). What is it?
This is a common problem in many/most programming languages. Each language has its own way of dealing with it.
P.S. Note, BTW, that in practice, you will need to do: flush stdout
in order to make sure the prompt gets out. But that is a small detail.
| The obvious way to do this would be:
| puts -nonewline "Enter something: "
| while { [gets ...] != -1 } {
| ...
| puts -nonewline "Enter something: "
| }
| But that is messy because you have to write the prompting command twice.
while {1} {
puts -nonewline "Enter something: "
if {[gets ...] < 0} {
break
}
}
In article <yga1r36is81.fsf@akutech.de>, Ralf Fassel <ralfixx@gmx.de> wrote:
* gazelle@shell.xmission.com (Kenny McCormack)
| The obvious way to do this would be:
| puts -nonewline "Enter something: "
| while { [gets ...] != -1 } {
| ...
| puts -nonewline "Enter something: "
| }
| But that is messy because you have to write the prompting command twice. >>
while {1} {
puts -nonewline "Enter something: "
if {[gets ...] < 0} {
break
}
}
It seems that there is no actual solution, only workarounds. Note that in
C, you can use the comma (,) operator to do this, like:
while (printf("Enter something: "),gets(...) != NULL) { ... }
(or something like that - don't nitpick it, I'm just trying to explain what I'm talking about here)
My reason for posting was to find out if there WAS a built-in way to do it, and the answer seems to be "NO".
Anyway, it sounds like putting it in a proc is probably the best workaround.
In article <yga1r36is81.fsf@akutech.de>, Ralf Fassel <ralfixx@gmx.de> wrote: >* gazelle@shell.xmission.com (Kenny McCormack)
| The obvious way to do this would be:
| puts -nonewline "Enter something: "
| while { [gets ...] != -1 } {
| ...
| puts -nonewline "Enter something: "
| }
| But that is messy because you have to write the prompting command twice.
while {1} {
puts -nonewline "Enter something: "
if {[gets ...] < 0} {
break
}
}
It seems that there is no actual solution, only workarounds. Note that in
C, you can use the comma (,) operator to do this, like:
while (printf("Enter something: "),gets(...) != NULL) { ... }
(or something like that - don't nitpick it, I'm just trying to explain what I'm talking about here)
My reason for posting was to find out if there WAS a built-in way to do it, and the answer seems to be "NO".
Anyway, it sounds like putting it in a proc is probably the best workaround.
In article <yga1r36is81.fsf@akutech.de>, Ralf Fassel <ralfixx@gmx.de> wrote:
* gazelle@shell.xmission.com (Kenny McCormack)It seems that there is no actual solution, only workarounds. Note that in
...
C, you can use the comma (,) operator to do this, like:
My reason for posting was to find out if there WAS a built-in way to do it, and the answer seems to be "NO".
Anyway, it sounds like putting it in a proc is probably the best workaround.
Anyway, it sounds like putting it in a proc is probably the best workaround.
Wrong, read Robert Heller's reply.
In article <snlfoe$oc9$1@gioia.aioe.org>,
Gerald Lester <Gerald.Lester@KnG-Consulting.net> wrote:
...
Anyway, it sounds like putting it in a proc is probably the best workaround.
Wrong, read Robert Heller's reply.
Yes, I see now that I was wrong to say that:
It sounds like putting it in a proc is probably the best workaround.
I did not know that (until I read it in this thread) you could put multiple commands inside a single [] (using the usual semicolon to separate them).
Thanks to Ian for posting that!
It seems that there is no actual solution, only workarounds.
Note that in C, you can use the comma (,) operator to do this, like:
while (printf("Enter something: "),gets(...) != NULL) { ... }
(or something like that - don't nitpick it, I'm just trying to
explain what I'm talking about here)
My reason for posting was to find out if there WAS a built-in way to
do it, and the answer seems to be "NO".
Thanks to Ian for posting that!
You missed the point -- putting it into a proc is not a "workaround"; it
is the way some people choose to do it.
No, I haven't, that's why I'm asking questions. If you won't help me,
why don't you just go find your lost manhood elsewhere.
Well, Tcl does have the comma operator, it's just spelt ";":
while { [puts -nonewline "Enter something: "; gets ...] != -1 } {
Personally though I prefer the proc or while {1} solutions.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (2 / 14) |
Uptime: | 210:42:17 |
Calls: | 6,619 |
Calls today: | 1 |
Files: | 12,168 |
Messages: | 5,317,248 |