Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Calm down, it's *____only* ones and zeroes.


devel / comp.lang.tcl / Re: Help with syntax sugar for arrays

SubjectAuthor
* Help with syntax sugar for arraysLuc
+- Help with syntax sugar for arraysRalf Fassel
`* Help with syntax sugar for arraysRich
 `* Help with syntax sugar for arrayssaitology9
  `- Help with syntax sugar for arrayssaitology9

1
Help with syntax sugar for arrays

<20221221055241.767b294e@lud1.home>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10410&group=comp.lang.tcl#10410

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!aioe.org!HESYh6hlKWfwzlzvFPLFyA.user.46.165.242.75.POSTED!not-for-mail
From: no...@no.no (Luc)
Newsgroups: comp.lang.tcl
Subject: Help with syntax sugar for arrays
Date: Wed, 21 Dec 2022 05:52:41 -0300
Organization: Aioe.org NNTP Server
Message-ID: <20221221055241.767b294e@lud1.home>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="33529"; posting-host="HESYh6hlKWfwzlzvFPLFyA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
X-Newsreader: Claws Mail 3.14.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu)
 by: Luc - Wed, 21 Dec 2022 08:52 UTC

I think I lifted this from wikit many winters ago:

# lists and arrays with sugar
set rgx {([0-9A-Za-z_]+):([0-9A-Za-z_]+)$}
if [regexp $rgx [lindex $args 0] -> foo _bar] {
upvar $foo _foo
# if it is array...
if [-a _foo] {return $_foo($_bar)}
# and if it is list...
return [lindex $_foo $_bar]
}

Goal:

% array set test {a 1 b 2 c 3 d 4}
% puts [test:a]
1 % puts [test:b]
2

How is writing [test:a] better than $test(a)?

It's better when 'test' is a variable (probably in a loop) whose value
is the name of an array (looping through a list of arrays) and I can write

[$test:a]

instead of

[set $test(a)]

especially when I want to access that array in the global scope from inside
a proc, which in proper Tcl would be

[set ::${::test}(a)]

Yikes!

But my proc often fails inside procs. Not always, but often, which puzzles me.

Is this practice dangerous? Why? Is there a better way?

I am using the yikes! method for now because it's safer, but I think it's a
monstrosity and really wish I could use a cleaner syntax.

Note I only use my overloaded 'unknown' proc and interp aliases in scripts
that I'm sure will never be shared.

--
Luc
>>

Re: Help with syntax sugar for arrays

<ygay1r1m0x4.fsf@akutech.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10411&group=comp.lang.tcl#10411

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralf...@gmx.de (Ralf Fassel)
Newsgroups: comp.lang.tcl
Subject: Re: Help with syntax sugar for arrays
Date: Wed, 21 Dec 2022 12:49:43 +0100
Lines: 30
Message-ID: <ygay1r1m0x4.fsf@akutech.de>
References: <20221221055241.767b294e@lud1.home>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net aEeGB3ZJ/nBHMQTi7dacYA58eQ0gX/KApzBY53evjmMoGwCMY=
Cancel-Lock: sha1:GbuHNpsDFWP5zx3sh+V8YCzdCHU= sha1:WF3aQa3hj31/HQq/Uf/FXLLG9h4=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
 by: Ralf Fassel - Wed, 21 Dec 2022 11:49 UTC

* Luc <no@no.no>
| It's better when 'test' is a variable (probably in a loop) whose value
| is the name of an array (looping through a list of arrays) and I can write
>
| [$test:a]
>
| instead of
>
| [set $test(a)]

Or rather [set ${test}(a)] if $test is the *name* of the array.

| especially when I want to access that array in the global scope from inside
| a proc, which in proper Tcl would be
>
| [set ::${::test}(a)]

IMHO the proper TCL way would be using upvar:

proc foo {global_arrayname} {
upvar \#0 $global_arrayname foo
puts "a in global array $global_arrayname is {$foo(a)}"
}
$ foo x
can't read "foo(a)": no such variable
$ array set x { a 1 b 2 }
$ foo x
a in global array x is {1}

R'

Re: Help with syntax sugar for arrays

<tnvft3$132ml$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10421&group=comp.lang.tcl#10421

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Help with syntax sugar for arrays
Date: Wed, 21 Dec 2022 17:31:15 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <tnvft3$132ml$1@dont-email.me>
References: <20221221055241.767b294e@lud1.home>
Injection-Date: Wed, 21 Dec 2022 17:31:15 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3f4718d035ce4159c17b6ad5c54e3c17";
logging-data="1149653"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1949O9VguD4MU32i8fPLUZ/"
User-Agent: tin/2.0.1-20111224 ("Achenvoir") (UNIX) (Linux/3.10.17 (x86_64))
Cancel-Lock: sha1:s2PvtOSQGrdtY+p1Kqtxduedluw=
 by: Rich - Wed, 21 Dec 2022 17:31 UTC

Luc <no@no.no> wrote:
> I think I lifted this from wikit many winters ago:
>
> # lists and arrays with sugar
> set rgx {([0-9A-Za-z_]+):([0-9A-Za-z_]+)$}
> if [regexp $rgx [lindex $args 0] -> foo _bar] {
> upvar $foo _foo
> # if it is array...
> if [-a _foo] {return $_foo($_bar)}
> # and if it is list...
> return [lindex $_foo $_bar]
> }
>
>
> Goal:
>
> % array set test {a 1 b 2 c 3 d 4}
> % puts [test:a]
> 1
> % puts [test:b]
> 2
>
>
> How is writing [test:a] better than $test(a)?

Some possible reasons:

1) code golf -- just to show the syntax flexibility of TCL;

2) for someone with a keyboard set for a non-english locale, possibly [
] and : are easier to type than $ ( and ) ?

Re: Help with syntax sugar for arrays

<tnvij8$n80$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10424&group=comp.lang.tcl#10424

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!aioe.org!Uw/OCQynP0ivtNQPQQJATg.user.46.165.242.75.POSTED!not-for-mail
From: saitolo...@gmail.com (saitology9)
Newsgroups: comp.lang.tcl
Subject: Re: Help with syntax sugar for arrays
Date: Wed, 21 Dec 2022 13:17:11 -0500
Organization: Aioe.org NNTP Server
Message-ID: <tnvij8$n80$1@gioia.aioe.org>
References: <20221221055241.767b294e@lud1.home> <tnvft3$132ml$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="23808"; posting-host="Uw/OCQynP0ivtNQPQQJATg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.3.2
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: saitology9 - Wed, 21 Dec 2022 18:17 UTC

On 12/21/2022 12:31 PM, Rich wrote:
> Luc <no@no.no> wrote:
>> I think I lifted this from wikit many winters ago:
>>
>> # lists and arrays with sugar
>> >> How is writing [test:a] better than $test(a)?
>
> Some possible reasons:
>
> 1) code golf -- just to show the syntax flexibility of TCL;
>
> 2) for someone with a keyboard set for a non-english locale, possibly [
> ] and : are easier to type than $ ( and ) ?
>

Another point, which I suspect is more likely, is this:

It tries to normalize both array and list retrievals, to make them look
identical. So, [var $x] and [lindex $var $x] are uniformly written as
[$var $x]. In some cases, this may be a useful abstraction, despite the
an additional proc call for each such access.

Re: Help with syntax sugar for arrays

<tnvj3a$sum$2@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10426&group=comp.lang.tcl#10426

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!aioe.org!Uw/OCQynP0ivtNQPQQJATg.user.46.165.242.75.POSTED!not-for-mail
From: saitolo...@gmail.com (saitology9)
Newsgroups: comp.lang.tcl
Subject: Re: Help with syntax sugar for arrays
Date: Wed, 21 Dec 2022 13:25:45 -0500
Organization: Aioe.org NNTP Server
Message-ID: <tnvj3a$sum$2@gioia.aioe.org>
References: <20221221055241.767b294e@lud1.home> <tnvft3$132ml$1@dont-email.me>
<tnvij8$n80$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="29654"; posting-host="Uw/OCQynP0ivtNQPQQJATg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.3.2
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: saitology9 - Wed, 21 Dec 2022 18:25 UTC

On 12/21/2022 1:17 PM, saitology9 wrote:
> Another point, which I suspect is more likely, is this:
>
> It tries to normalize both array and list retrievals, to make them look
> identical.  So, [var $x] and [lindex $var $x] are uniformly written as

The last sentence should read:

"So, $var($x) and [lindex $var $x] are uniformly written as"


devel / comp.lang.tcl / Re: Help with syntax sugar for arrays

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor