Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

She is not refined. She is not unrefined. She keeps a parrot. -- Mark Twain


comp / comp.lang.tcl / Re: Array get element with default (no error if not exist)

SubjectAuthor
* Array get element with default (no error if not exist)RodionGork
+* Re: Array get element with default (no error if not exist)Ralf Fassel
|`* Re: Array get element with default (no error if not exist)RodionGork
| `* Re: Array get element with default (no error if not exist)Rich
|  `* Re: Array get element with default (no error if not exist)RodionGork
|   `- Re: Array get element with default (no error if not exist)Rich
`* Re: Array get element with default (no error if not exist)Emiliano
 `- Re: Array get element with default (no error if not exist)Ralf Fassel

1
Subject: Array get element with default (no error if not exist)
From: RodionGork
Newsgroups: comp.lang.tcl
Organization: novaBBS
Date: Fri, 16 Aug 2024 07:10 UTC
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: rodiongork@github.com (RodionGork)
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
View all headers

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

Subject: Re: Array get element with default (no error if not exist)
From: Ralf Fassel
Newsgroups: comp.lang.tcl
Date: Fri, 16 Aug 2024 09:11 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralfixx@gmx.de (Ralf Fassel)
Newsgroups: comp.lang.tcl
Subject: Re: Array get element with default (no error if not exist)
Date: Fri, 16 Aug 2024 11:11:29 +0200
Lines: 41
Message-ID: <ygao75szvb2.fsf@akutech.de>
References: <962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net 2SOj6ud0n0LWWvYeHw9G/ACFd+9AR7IreQ4XwQUq0rQcuy9bs=
Cancel-Lock: sha1:b3XvmiTsGjL1cHsZPWdy+gRnuyk= sha1:EFxB0+RyEMITAJth8S5h9W093kM= sha256:eY8Lkdvvwb/yem8qe7uwF1tv+9rq/nAMahNsP2OM5HY=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
View all headers

* RodionGork <rodiongork@github.com>
| Is there some motivation why some command for get-with-default is not
| implemented, e.g.
>
| puts [array peek $a 2 "default value"]

Most probably because up to now nobody required it so badly that s/he
implemented it, since the [info exists] approach is not too bad IMHO.

proc array_peek {arr key default} {
upvar $arr a
if {![array exists a] || ![info exists a($key)]} {
return $default
}
return $a($key)
}
array set a {1 one}
array_peek a 1 "default value"
=> one
array_peek a 2 "default value"
=> default value

Or you could set up a read-trace on the array to provide non-existing values:

array set a {}
trace add variable a read set_array_default
proc set_array_default {name1 name2 op} {
upvar a $name1
# puts stderr "set_array {$name1} {$name2} {$op}"
if {![info exists a($name2)]} {
set a($name2) "default"
}
}
set a(2)
=> default

Though that would alter the array and make the key exists which might
not be what you want.

HTH
R'

Subject: Re: Array get element with default (no error if not exist)
From: Emiliano
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 16 Aug 2024 14:16 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: emil.g@example.invalid (Emiliano)
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)
View all headers

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

Subject: Re: Array get element with default (no error if not exist)
From: Ralf Fassel
Newsgroups: comp.lang.tcl
Date: Fri, 16 Aug 2024 14:54 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralfixx@gmx.de (Ralf Fassel)
Newsgroups: comp.lang.tcl
Subject: Re: Array get element with default (no error if not exist)
Date: Fri, 16 Aug 2024 16:54:01 +0200
Lines: 21
Message-ID: <ygajzggzfg6.fsf@akutech.de>
References: <962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com>
<20240816111604.6e47340154e68f982aa12c3f@example.invalid>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net B103Gmw2/Cu4fxYWtZPjaA9K+X7QG174ZKMIwSsDowD1jIrhE=
Cancel-Lock: sha1:2M8SWTreNtiZ9C3W491iPA5BVS8= sha1:fQRMZOl+zzRyWjd6MH5KMH7lDdo= sha256:E3+PrWmAxKCskqh64T+wJs25dekjQdR85ZV8WKKm6Ic=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
View all headers

* Emiliano <emil.g@example.invalid>
| On Fri, 16 Aug 2024 07:10:30 +0000
| RodionGork <rodiongork@github.com> wrote:
| > 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

Obviouosly in this context the OP meant "array exists" with respect to a
specific key, i.e.

array exists arrayname key
=> return true if arrayname is an array and the key exists in that array.

R'

Subject: Re: Array get element with default (no error if not exist)
From: RodionGork
Newsgroups: comp.lang.tcl
Organization: novaBBS
Date: Sat, 17 Aug 2024 06:13 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!panix!weretis.net!feeder9.news.weretis.net!news.nk.ca!rocksolid2!i2pn2.org!.POSTED!not-for-mail
From: rodiongork@github.com (RodionGork)
Newsgroups: comp.lang.tcl
Subject: Re: Array get element with default (no error if not exist)
Date: Sat, 17 Aug 2024 06:13:34 +0000
Organization: novaBBS
Message-ID: <850513f1dcb6a0714bffa99c137f69f9@www.novabbs.com>
References: <962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com> <ygao75szvb2.fsf@akutech.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="2860005"; mail-complaints-to="usenet@i2pn2.org";
posting-account="FK20xSPHkh3K4vnO8u2oiUWWGFHCzgkK4jO78trwjP4";
User-Agent: Rocksolid Light
X-Rslight-Site: $2y$10$srmf7hPP4QHofoN.kk3qsezI3JvFI.M8aglKXvgc/XvwgFp99rjtW
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 32ecc5e38066f1efcacd4ff0a351d3cb52726446
View all headers

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 :)

--
to email me substitute github with gmail please

Subject: Re: Array get element with default (no error if not exist)
From: Rich
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sat, 17 Aug 2024 16:17 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Array get element with default (no error if not exist)
Date: Sat, 17 Aug 2024 16:17:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <v9qiej$1vbpg$1@dont-email.me>
References: <962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com> <ygao75szvb2.fsf@akutech.de> <850513f1dcb6a0714bffa99c137f69f9@www.novabbs.com>
Injection-Date: Sat, 17 Aug 2024 18:17:23 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="c77a774aa1439979a56b68d357ab6f19";
logging-data="2076464"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NDOZFrxx9f832AD1Anlwr"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:FHiDd8WmpuG8bXYnphIZuM8ct/4=
View all headers

RodionGork <rodiongork@github.com> wrote:
> 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 :)

[info exists] is used for determining if a variable exists.

A Tcl array is built such that it is a collection of individual
variables referenced together as a set (this is also the reason why you
can't "pass" an array to a proc, nor [return] an array from a proc in
the same way you can pass/return a dict [1]).

Given that the array is merely a set of individual variables, someone,
way back in the distant past, decided it was more consistent to use the
"does this variable exist" command to determine if an individual
variable existed in an array.

[1] You can pass in the name of the array, and use upvar to access it, or
you can convert to a string and pass in the string. And to [return]
you have to convert to a string ([array get]) to return one. But this
does not work:

proc handle {arr} {
puts "I have array [array get arr]"
}

set array(1) something

handle $array

You get "can't read "array": variable is array" as an error message.

This difference is also why you can [trace] individual array elements
(they are individual variables) but cannot [trace] individual dict
elements (they are just entries inside the dict variable).

Subject: Re: Array get element with default (no error if not exist)
From: RodionGork
Newsgroups: comp.lang.tcl
Organization: novaBBS
Date: Sun, 18 Aug 2024 14:10 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: rodiongork@github.com (RodionGork)
Newsgroups: comp.lang.tcl
Subject: Re: Array get element with default (no error if not exist)
Date: Sun, 18 Aug 2024 14:10:58 +0000
Organization: novaBBS
Message-ID: <a41ab7e0d5109e5d55f5cce6a8069813@www.novabbs.com>
References: <962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com> <ygao75szvb2.fsf@akutech.de> <850513f1dcb6a0714bffa99c137f69f9@www.novabbs.com> <v9qiej$1vbpg$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="3011025"; 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$IwOZcFsckI.tU3K6MDA1LenfqSYtlfnMQBhrkPbkHVqSF4iPbb2DO
X-Rslight-Posting-User: 32ecc5e38066f1efcacd4ff0a351d3cb52726446
View all headers

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

--
to email me substitute github with gmail please

Subject: Re: Array get element with default (no error if not exist)
From: Rich
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sun, 18 Aug 2024 16:43 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Array get element with default (no error if not exist)
Date: Sun, 18 Aug 2024 16:43:41 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <v9t8bt$2euft$1@dont-email.me>
References: <962f50d6039d29a1bcdd98d8931988a3@www.novabbs.com> <ygao75szvb2.fsf@akutech.de> <850513f1dcb6a0714bffa99c137f69f9@www.novabbs.com> <v9qiej$1vbpg$1@dont-email.me> <a41ab7e0d5109e5d55f5cce6a8069813@www.novabbs.com>
Injection-Date: Sun, 18 Aug 2024 18:43:41 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="27a8b87db7433b5fa1dd735d9d0e6b28";
logging-data="2587133"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Puax/xCEEw78HnKCaQ6Qn"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:197VsDzE5mpmNHr2PCiWQAzhpP0=
View all headers

RodionGork <rodiongork@github.com> wrote:
> 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

Because the [global] command works with names of variables, not the
actual variables. It links the global variable with the given name, to
a local variable of the same name, inside the proc. If the global name
references an array, then the new local name inside the proc references
the same array.

1

rocksolid light 0.9.8
clearnet tor