• Help with syntax sugar for arrays

    From Luc@21:1/5 to All on Wed Dec 21 05:52:41 2022
    I think I lifted this from wikit many winters ago:

    # lists and arrays with sugar
    set rgx {([0-9A-Za-z_]+):([0-9A-Za-z_]+)$}
    if [regexp $rgx [lindex $args 0] -> foo _bar] {
    upvar $foo _foo
    # if it is array...
    if [-a _foo] {return $_foo($_bar)}
    # and if it is list...
    return [lindex $_foo $_bar]
    }


    Goal:

    % array set test {a 1 b 2 c 3 d 4}
    % puts [test:a]
    1
    % puts [test:b]
    2


    How is writing [test:a] better than $test(a)?

    It's better when 'test' is a variable (probably in a loop) whose value
    is the name of an array (looping through a list of arrays) and I can write

    [$test:a]

    instead of

    [set $test(a)]

    especially when I want to access that array in the global scope from inside
    a proc, which in proper Tcl would be

    [set ::${::test}(a)]

    Yikes!

    But my proc often fails inside procs. Not always, but often, which puzzles me.


    Is this practice dangerous? Why? Is there a better way?

    I am using the yikes! method for now because it's safer, but I think it's a monstrosity and really wish I could use a cleaner syntax.

    Note I only use my overloaded 'unknown' proc and interp aliases in scripts
    that I'm sure will never be shared.

    --
    Luc


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Wed Dec 21 12:49:43 2022
    * Luc <[email protected]>
    | It's better when 'test' is a variable (probably in a loop) whose value
    | is the name of an array (looping through a list of arrays) and I can write

    | [$test:a]

    | instead of

    | [set $test(a)]

    Or rather [set ${test}(a)] if $test is the *name* of the array.

    | especially when I want to access that array in the global scope from inside
    | a proc, which in proper Tcl would be

    | [set ::${::test}(a)]

    IMHO the proper TCL way would be using upvar:

    proc foo {global_arrayname} {
    upvar \#0 $global_arrayname foo
    puts "a in global array $global_arrayname is {$foo(a)}"
    }
    $ foo x
    can't read "foo(a)": no such variable
    $ array set x { a 1 b 2 }
    $ foo x
    a in global array x is {1}

    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to Luc on Wed Dec 21 17:31:15 2022
    Luc <[email protected]> wrote:
    I think I lifted this from wikit many winters ago:

    # lists and arrays with sugar
    set rgx {([0-9A-Za-z_]+):([0-9A-Za-z_]+)$}
    if [regexp $rgx [lindex $args 0] -> foo _bar] {
    upvar $foo _foo
    # if it is array...
    if [-a _foo] {return $_foo($_bar)}
    # and if it is list...
    return [lindex $_foo $_bar]
    }


    Goal:

    % array set test {a 1 b 2 c 3 d 4}
    % puts [test:a]
    1
    % puts [test:b]
    2


    How is writing [test:a] better than $test(a)?

    Some possible reasons:

    1) code golf -- just to show the syntax flexibility of TCL;

    2) for someone with a keyboard set for a non-english locale, possibly [
    ] and : are easier to type than $ ( and ) ?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From saitology9@21:1/5 to Rich on Wed Dec 21 13:17:11 2022
    On 12/21/2022 12:31 PM, Rich wrote:
    Luc <[email protected]> wrote:
    I think I lifted this from wikit many winters ago:

    # lists and arrays with sugar
    How is writing [test:a] better than $test(a)?

    Some possible reasons:

    1) code golf -- just to show the syntax flexibility of TCL;

    2) for someone with a keyboard set for a non-english locale, possibly [
    ] and : are easier to type than $ ( and ) ?


    Another point, which I suspect is more likely, is this:

    It tries to normalize both array and list retrievals, to make them look identical. So, [var $x] and [lindex $var $x] are uniformly written as
    [$var $x]. In some cases, this may be a useful abstraction, despite the
    an additional proc call for each such access.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From saitology9@21:1/5 to All on Wed Dec 21 13:25:45 2022
    On 12/21/2022 1:17 PM, saitology9 wrote:
    Another point, which I suspect is more likely, is this:

    It tries to normalize both array and list retrievals, to make them look identical.  So, [var $x] and [lindex $var $x] are uniformly written as


    The last sentence should read:

    "So, $var($x) and [lindex $var $x] are uniformly written as"

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