• Re: How to prompt before get?

    From Gerald Lester@21:1/5 to Kenny McCormack on Tue Nov 23 13:00:27 2021
    On 11/23/21 12:58 PM, Kenny McCormack wrote:
    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.

    Read the proc man/help page.


    --
    +----------------------------------------------------------------------+
    | Gerald W. Lester, President, KNG Consulting LLC |
    | Email: Gerald.Lester@kng-consulting.net | +----------------------------------------------------------------------+

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to All on Tue Nov 23 18:58:22 2021
    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.

    --

    "If God wanted us to believe in him, he'd exist."

    (Linda Smith on "10 Funniest Londoners", TimeOut, 23rd June, 2005.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Tue Nov 23 20:31:42 2021
    * 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
    }
    }

    HTH
    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to ralfixx@gmx.de on Wed Nov 24 12:29:05 2021
    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.

    --
    The scent of awk programmers is a lot more attractive to women than
    the scent of perl programmers.

    (Mike Brennan, quoted in the "GAWK" manual)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ian Braithwaite@21:1/5 to Kenny McCormack on Wed Nov 24 13:43:25 2021
    On 24/11/2021 13.29, Kenny McCormack wrote:
    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.


    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.

    -Ian

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Heller@21:1/5 to Kenny McCormack on Wed Nov 24 06:54:57 2021
    At Wed, 24 Nov 2021 12:29:05 -0000 (UTC) gazelle@shell.xmission.com (Kenny McCormack) wrote:


    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.

    Putting any "complex" or "compound" operation into a proc is in fact the stock (built-in?) solution for Tcl. This is not really a "wordaround" but more of a standard operating procedure. That is, a proc can be used to extend the language.

    rename gets _gets
    proc gets {{in stdin} {buffername {}} args} {
    if { {set i [lsearch -exact $args -prompt]} >= 0} {
    puts -nonewline [lindex $args [expr {$i + 1}]]
    flush stdout
    }
    if {$buffername eq {}} {
    return [_gets $in]
    } else {
    upvar $buffername buffer
    return [_gets $in buffer]
    }
    }



    --
    Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
    Deepwoods Software -- Custom Software Services
    http://www.deepsoft.com/ -- Linux Administration Services
    heller@deepsoft.com -- Webhosting Services

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gerald Lester@21:1/5 to Kenny McCormack on Wed Nov 24 07:46:53 2021
    On 11/24/21 6:29 AM, Kenny McCormack wrote:
    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:

    Tcl is NOT C. Tcl is NOT Java. Tcl is NOT COBAL.

    Learn the Twelve Syntax Rules.

    There are NO RESERVED WORDS.

    A proc is just a command written in Tcl.


    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.

    Wrong, read Robert Heller's reply.

    --
    +----------------------------------------------------------------------+
    | Gerald W. Lester, President, KNG Consulting LLC |
    | Email: Gerald.Lester@kng-consulting.net | +----------------------------------------------------------------------+

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Wed Nov 24 15:27:33 2021
    * Ian Braithwaite <ian@braithwaite.dk>
    | Well, Tcl does have the comma operator, it's just spelt ";":

    :-)))

    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to Gerald.Lester@KnG-Consulting.net on Wed Nov 24 15:28:00 2021
    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!

    --
    The people who tell us to be proud of the white race are the ones who make
    us the most embarrassed by it.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gerald Lester@21:1/5 to Kenny McCormack on Wed Nov 24 11:21:44 2021
    On 11/24/21 9:28 AM, Kenny McCormack wrote:
    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!

    You missed the point -- putting it into a proc is not a "workaround"m it
    is a proper way for the language.


    --
    +----------------------------------------------------------------------+
    | Gerald W. Lester, President, KNG Consulting LLC |
    | Email: Gerald.Lester@kng-consulting.net | +----------------------------------------------------------------------+

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to Kenny McCormack on Wed Nov 24 17:48:57 2021
    Kenny McCormack <gazelle@shell.xmission.com> wrote:
    It seems that there is no actual solution, only workarounds.

    Not quite correct

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

    Incorrect. Substitute ; for , and Tcl allows pretty much what C allows
    above:

    while {[ puts "prompt"; flush stdout; gets stdin input] != -1} {
    puts stderr input='$input'
    }

    That is if you 'really' want the prompt puts inside the while
    condition.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to Gerald.Lester@KnG-Consulting.net on Wed Nov 24 17:46:10 2021
    In article <snlsb8$1i76$1@gioia.aioe.org>,
    Gerald Lester <Gerald.Lester@KnG-Consulting.net> wrote:
    ...
    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.

    I didn't "miss" the point. I was rubbing your nose in the fact that you
    missed my point.

    In my view, it IS a workaround. I prefer the inline way of doing 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.

    CLC in a nutshell.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to ian@braithwaite.dk on Wed Nov 24 18:46:37 2021
    In article <619e3371$0$692$14726298@news.sunsite.dk>,
    Ian Braithwaite <ian@braithwaite.dk> wrote:
    ...
    Well, Tcl does have the comma operator, it's just spelt ";":

    while { [puts -nonewline "Enter something: "; gets ...] != -1 } {

    Thank you. I didn't know you could string commands together inside of []. That's what I was looking for.

    Personally though I prefer the proc or while {1} solutions.

    To each his own.

    Personally, I think the "while 1" idea is silly, but again, to each his own.

    --
    The randomly chosen signature file that would have appeared here is more than 4 lines long. As such, it violates one or more Usenet RFCs. In order to remain in compliance with said RFCs, the actual sig can be found at the following URL:
    http://user.xmission.com/~gazelle/Sigs/Aspergers

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)