• Question to dict set

    From greg@gregor.ebbing@gmx.de to comp.lang.tcl on Sat Feb 24 14:47:05 2024
    From Newsgroup: comp.lang.tcl

    I don't understand why the error occurs in my script, or where exactly.

    missing value to go with key

    Why does an error occur in Scenario 4 when I try to use dict set on a
    variable initially defined as a list with the same keys and values I
    want to add?
    Why does no error occur in Scenario 5 when the initial list contains
    different keys and values?
    The script;

    #! /usr/bin/env tclsh

    package require dicttool

    puts "info patchlevel [info patchlevel]"
    puts "tclplatform(os) $tcl_platform(os)"
    puts "tclplatform(osVersion) $tcl_platform(osVersion)"

    #1.
    puts "\n1. without error different variable names dvar1 and dvar2"
    set dvar1 [dict create k1 v1 k2 v2]
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"

    dict set dvar2 k3 v3 k4 v4
    puts "dvar2: $dvar2 is: [dict is_dict $dvar2]"

    #2.
    unset dvar1
    unset dvar2
    puts "\n2. with error same variable name dvar1"
    set dvar1 [dict create k1 v1 k2 v2]
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"

    catch {dict set dvar1 k3 v3 k4 v4} err
    puts "err: $err"
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"

    #3.
    unset dvar1
    unset err
    puts "\n3. with error same variable name dvar1 but same keys and values
    dict create"
    set dvar1 [dict create k1 v1 k2 v2]
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"
    puts "length dvar1: [llength $dvar1]"
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"
    set dvar1 [list k1 v1 k2 v2]
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"
    puts "length dvar1: [llength $dvar1]"

    catch {dict set dvar1 k3 v3 k4 v4} err
    puts "err: $err"
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"


    #4.
    unset dvar1
    unset err
    puts "\n4. with error same variable name dvar1 but same keys and values
    dict set"
    set dvar1 [dict create k1 v1 k2 v2]
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"
    puts "length dvar1: [llength $dvar1]"
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"
    set dvar1 [list k3 v3 k4 v4]
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"
    puts "length dvar1: [llength $dvar1]"

    catch {dict set dvar1 k3 v3 k4 v4} err
    puts "err: $err"
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"

    #5.
    unset dvar1
    unset err
    puts "\n5. without error same variable name dvar1 but different keys and values"
    set dvar1 [dict create k1 v1 k2 v2]
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"
    puts "length dvar1: [llength $dvar1]"
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"
    set dvar1 [list k5 v5 k6 v6]
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"
    puts "length dvar1: [llength $dvar1]"

    catch {dict set dvar1 k3 v3 k4 v4} err
    puts "err: $err"
    puts "dvar1: $dvar1 is: [dict is_dict $dvar1]"


    if {0} {
    output:

    info patchlevel 8.6.13
    tclplatform(os) Linux
    tclplatform(osVersion) 6.6.15-amd64

    1. without error different variable names dvar1 and dvar2
    dvar1: k1 v1 k2 v2 is: 1
    dvar2: k3 {v3 {k4 v4}} is: 1

    2. with error same variable name dvar1
    dvar1: k1 v1 k2 v2 is: 1
    err: k1 v1 k2 v2 k3 {v3 {k4 v4}}
    dvar1: k1 v1 k2 v2 k3 {v3 {k4 v4}} is: 1

    3. with error same variable name dvar1 but same keys and values dict create dvar1: k1 v1 k2 v2 is: 1
    length dvar1: 4
    dvar1: k1 v1 k2 v2 is: 1
    dvar1: k1 v1 k2 v2 is: 1
    length dvar1: 4
    err: k1 v1 k2 v2 k3 {v3 {k4 v4}}
    dvar1: k1 v1 k2 v2 k3 {v3 {k4 v4}} is: 1

    4. with error same variable name dvar1 but same keys and values dict set
    dvar1: k1 v1 k2 v2 is: 1
    length dvar1: 4
    dvar1: k1 v1 k2 v2 is: 1
    dvar1: k3 v3 k4 v4 is: 1
    length dvar1: 4
    err: missing value to go with key
    dvar1: k3 v3 k4 v4 is: 1

    5. without error same variable name dvar1 but different keys and values
    dvar1: k1 v1 k2 v2 is: 1
    length dvar1: 4
    dvar1: k1 v1 k2 v2 is: 1
    dvar1: k5 v5 k6 v6 is: 1
    length dvar1: 4
    err: k5 v5 k6 v6 k3 {v3 {k4 v4}}
    dvar1: k5 v5 k6 v6 k3 {v3 {k4 v4}} is: 1

    }
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Rich@rich@example.invalid to comp.lang.tcl on Sat Feb 24 21:26:52 2024
    From Newsgroup: comp.lang.tcl

    greg <gregor.ebbing@gmx.de> wrote:
    I don't understand why the error occurs in my script, or where exactly.

    missing value to go with key

    Why does an error occur in Scenario 4 when I try to use dict set on a variable initially defined as a list with the same keys and values I
    want to add?

    #4.
    set dvar1 [list k3 v3 k4 v4]

    Creates a list of four elements, which when shimmered to a dict will
    produce a dict with two keys (k3, k4) each containing one value (v3,
    v4) respectively.

    dict set dvar1 k3 v3 k4 v4

    Attempts to set the chain of keys k3->v3->k4 to the value v4. See the
    'dict set' subcommand in the dict manpage, the above is not two keys
    with two values but a three level nested dict.

    When [dict] looks at dvar1, it finds a k3 key at the top level, it then
    looks at the value of k3, finds only "v3" (not a dict) and errors out
    because "v3" is not a key.


    Why does no error occur in Scenario 5 when the initial list contains different keys and values?

    #5.
    set dvar1 [list k5 v5 k6 v6]

    Creates a list containing four elements, which when shimmered to a
    dict, is a dict with two keys (k5, k6) with one value each (v5, v6).

    dict set dvar1 k3 v3 k4 v4

    Attempts to set the chain of nested keys k3->v3->k4 to the value v4.

    When [dict] looks at dvar1, it finds no key "k3", so it creates a new
    key "k3", then inside "k3" it creates a new nested dict with a key of
    "v3", then inside "v3" it creates a new nested dict with a key of "k4"
    and a value of "v4". No error because everything created was all new.


    Your test key/value names imply that you think that this:

    dict set d1 k1 v1 k2 v2

    produces this:

    {k1 v1} {k2 v2}

    When in fact it produces this:

    {k1 {v1 {k2 v2}}}

    Those two are two very different dicts. One is a flat dict with two
    keys, the other is a three level deep hierarchical dict.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From greg@gregor.ebbing@gmx.de to comp.lang.tcl on Sun Feb 25 07:10:16 2024
    From Newsgroup: comp.lang.tcl

    Am 24.02.24 um 22:26 schrieb Rich:
    ..
    When [dict] looks at dvar1, it finds a k3 key at the top level, it then
    looks at the value of k3, finds only "v3" (not a dict) and errors out
    because "v3" is not a key.



    Ok, now I understand,
    thanks
    Greg

    --- Synchronet 3.20a-Linux NewsLink 1.114