Hi Friends!
Still making my first feeble steps in TCL so please excuse me if this is naive or was asked multiple times.
Attempt to fetch by non-existing key in "associative array" results in
error, e.g.
set a(1) 5
puts $a(2) ;# yields error
the workaround seems to be [info exists ::a(2)] which feels a bit remote
from other "array" commands.
Is there some motivation why some command for get-with-default is not implemented, e.g.
puts [array peek $a 2 "default value"]
Popular use-case for this would be creating map where elements are
updated (like counter of words etc) - though I found this is cleverly
covered by "incr" and "append" commands properly behaving
when element to be incremented or appended does not exist yet.
But I suspect there are other situations when such a command may be
handy.
Also why [array exists ...] command does not exist (while [dict exists
..] does)? Perhaps there is something about no good syntax for it due
to how arrays are implemented?
Friends, thanks for all the replies!
And for suggestions providing insight on techniques of which I wasn't
aware of, especially that "read trace".
Yep, I definitely meant something like [array element_exists ...] rather
than [array exists ...] which seems a bit inconsistent with [dict exists
..] but of course it is not a big issue.
My curiosity was mostly satisfied, thanks! The only small thing I still haven't grasped well is why it happened that checking for element in
array found its way into [info exists...] rather than some [array ...] subcommands. I suspected there was something hindering implementing it
there, but probably it just, well, so happened historically :)
Thank you, this definitely makes sense!
I remember about issue with passing/returning array in functions. By the
way I still am not sure why it works with global for example... if they
are actually different variables, every key-pair :) e.g. like here:
https://github.com/RodionGork/languages-benchmark/blob/main/02-primes/primes_a.tcl#L7
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 991 |
Nodes: | 10 (0 / 10) |
Uptime: | 119:47:19 |
Calls: | 12,958 |
Files: | 186,574 |
Messages: | 3,265,639 |