Deutsch English Français Italiano |
<20240816111604.6e47340154e68f982aa12c3f@example.invalid> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Emiliano <emil.g@example.invalid> Newsgroups: comp.lang.tcl Subject: Re: Array get element with default (no error if not exist) Date: Fri, 16 Aug 2024 11:16:04 -0300 Organization: A noiseless patient Spider Lines: 57 Message-ID: <20240816111604.6e47340154e68f982aa12c3f@example.invalid> References: <962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Date: Fri, 16 Aug 2024 16:16:05 +0200 (CEST) Injection-Info: dont-email.me; posting-host="851377c2038e7e3fd050a9fe2517b2f2"; logging-data="1541934"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+PlApkWgNEcRFABE6ZSww5WdFAkPuYSak=" Cancel-Lock: sha1:JBl63ClKfROS83ofBNpZ7/6nrlg= X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Bytes: 2593 On Fri, 16 Aug 2024 07:10:30 +0000 RodionGork <rodiongork@github.com> wrote: > 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. Already there in 8.7/9.0 % array default set foo NOSUCHVALUE % set foo(1) NOSUCHVALUE % info exists foo(1) 0 8.7/9.0 also adds [dict getwithdefault] (aka [dict getdef]) for dict values. > 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? The [array exists] command is there since ... forever % array exists foo 1 % array exists nosucharray 0 Well, at least since 8.4 . See https://www.tcl-lang.org/man/tcl8.4/TclCmd/array.htm -- Emiliano