Deutsch   English   Français   Italiano  
<962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.misty.com!weretis.net!feeder9.news.weretis.net!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: RodionGork <rodiongork@github.com>
Newsgroups: comp.lang.tcl
Subject: Array get element with default (no error if not exist)
Date: Fri, 16 Aug 2024 07:10:30 +0000
Organization: novaBBS
Message-ID: <962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
	logging-data="2749678"; mail-complaints-to="usenet@i2pn2.org";
	posting-account="FK20xSPHkh3K4vnO8u2oiUWWGFHCzgkK4jO78trwjP4";
User-Agent: Rocksolid Light
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Site: $2y$10$hF6jFpz.Tecjxg/cdL7R/eIcMEgo0Xq7E6mhp.v4vSX8LgHPTKh8S
X-Rslight-Posting-User: 32ecc5e38066f1efcacd4ff0a351d3cb52726446
Bytes: 1962
Lines: 32

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