Array get element with default (no error if not exist)

Liste des GroupesRevenir à cl tcl 
Sujet : Array get element with default (no error if not exist)
De : rodiongork (at) *nospam* github.com (RodionGork)
Groupes : comp.lang.tcl
Date : 16. Aug 2024, 08:10:30
Autres entêtes
Organisation : novaBBS
Message-ID : <962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com>
User-Agent : Rocksolid Light
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?
--
to email me substitute github with gmail please

Date Sujet#  Auteur
16 Aug 24 * Array get element with default (no error if not exist)8RodionGork
16 Aug 24 +* Re: Array get element with default (no error if not exist)5Ralf Fassel
17 Aug 24 i`* Re: Array get element with default (no error if not exist)4RodionGork
17 Aug 24 i `* Re: Array get element with default (no error if not exist)3Rich
18 Aug 24 i  `* Re: Array get element with default (no error if not exist)2RodionGork
18 Aug 24 i   `- Re: Array get element with default (no error if not exist)1Rich
16 Aug 24 `* Re: Array get element with default (no error if not exist)2Emiliano
16 Aug 24  `- Re: Array get element with default (no error if not exist)1Ralf Fassel

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal