Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  nodelist  faq  login

MSDOS is not dead, it just smells that way. -- Henry Spencer


programming / comp.lang.tcl / Re: work with nested struct in proc in TCL

SubjectAuthor
* work with nested struct in proc in TCLOleg Nemanov
+- Re: work with nested struct in proc in TCLOleg Nemanov
+* Re: work with nested struct in proc in TCLsaitology9
|`* Re: work with nested struct in proc in TCLOleg Nemanov
| `* Re: work with nested struct in proc in TCLsaitology9
|  `* Re: work with nested struct in proc in TCLOleg Nemanov
|   +* Re: work with nested struct in proc in TCLsaitology9
|   |`- Re: work with nested struct in proc in TCLsaitology9
|   `* Re: work with nested struct in proc in TCLRich
|    `- Re: work with nested struct in proc in TCLOleg Nemanov
+* Re: work with nested struct in proc in TCLRich
|`- Re: work with nested struct in proc in TCLOleg Nemanov
`- Re: work with nested struct in proc in TCLSchelte

1
Subject: work with nested struct in proc in TCL
From: Oleg Nemanov
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 14:03 UTC
X-Received: by 2002:a05:6214:29e7:b0:455:28d0:8a7 with SMTP id jv7-20020a05621429e700b0045528d008a7mr513935qvb.37.1650636227991;
Fri, 22 Apr 2022 07:03:47 -0700 (PDT)
X-Received: by 2002:a4a:ac01:0:b0:324:910a:5d04 with SMTP id
p1-20020a4aac01000000b00324910a5d04mr1605891oon.87.1650636227678; Fri, 22 Apr
2022 07:03:47 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!3.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 07:03:47 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=194.190.114.28; posting-account=RPJNegoAAAAUgD_yLdrci9D1ZtZ1oI0L
NNTP-Posting-Host: 194.190.114.28
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
Subject: work with nested struct in proc in TCL
From: oleg.o.n...@gmail.com (Oleg Nemanov)
Injection-Date: Fri, 22 Apr 2022 14:03:47 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 37
View all headers
Hi, all.

I have some package where some procs get as argument  a dict with nested dicts. These procs call another internal procs that get as argument this dict too, but needs only a subdict to do its work. I can't pass this subdict by name,
because i can't modify it in place, in this case. And now my internal procs
should know the full path to subdict they need to do its work. E.g.:

set ctx [dict create src [dict create SOME_DATA] dst [dict create SOME_DAT]]
proc do_work {_ctx} {
  upvar $_ctx ctx
  ...
  src_proc1 ctx
  dst_proc1 ctx
  ...
}

proc src_proc1 {_ctx} {
  upvar $_ctx ctx
  dict set ctx src SOME_KEY 1
}

proc dst_proc1 {_ctx} {
  upvar $_ctx ctx
  dict set ctx dst SOME_KEY 1
}

I want to pass to an internal proc just needed subdist like(thus, internal procs will be know only keys its needed for work):

proc do_work {_ctx} {
  upvar $_ctx ctx
  ...
  src_proc1 [dict get $ctx src]
  dst_proc1 [dict get $ctx dst]
  ...
}

But i can't modify this data in src_proc1/dst_proc1 in place, in this case.

So, how you solve these situations?


Subject: Re: work with nested struct in proc in TCL
From: Oleg Nemanov
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 14:15 UTC
References: 1
X-Received: by 2002:a05:6214:c2b:b0:446:512a:26f7 with SMTP id a11-20020a0562140c2b00b00446512a26f7mr3855566qvd.38.1650636924032;
Fri, 22 Apr 2022 07:15:24 -0700 (PDT)
X-Received: by 2002:a9d:6488:0:b0:603:4051:da06 with SMTP id
g8-20020a9d6488000000b006034051da06mr1816085otl.298.1650636923731; Fri, 22
Apr 2022 07:15:23 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 07:15:23 -0700 (PDT)
In-Reply-To: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=194.190.114.28; posting-account=RPJNegoAAAAUgD_yLdrci9D1ZtZ1oI0L
NNTP-Posting-Host: 194.190.114.28
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <68dc95ef-7964-4876-bb6a-caa04311f460n@googlegroups.com>
Subject: Re: work with nested struct in proc in TCL
From: oleg.o.n...@gmail.com (Oleg Nemanov)
Injection-Date: Fri, 22 Apr 2022 14:15:24 +0000
Content-Type: text/plain; charset="UTF-8"
View all headers
In C i just pass a pointer to a nested struct into an internal functions and, thus, this function works only with a data they know and need.
This function can change it during a work. How can i achieve something like this in TCL?


Subject: Re: work with nested struct in proc in TCL
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Organization: Aioe.org NNTP Server
Date: Fri, 22 Apr 2022 14:32 UTC
References: 1
Path: i2pn2.org!i2pn.org!aioe.org!a5rWVvs5S5ZXUwkNcVnRMw.user.46.165.242.91.POSTED!not-for-mail
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Subject: Re: work with nested struct in proc in TCL
Date: Fri, 22 Apr 2022 10:32:41 -0400
Organization: Aioe.org NNTP Server
Message-ID: <t3ueaa$1noh$1@gioia.aioe.org>
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="57105"; posting-host="a5rWVvs5S5ZXUwkNcVnRMw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.6.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
View all headers
On 4/22/22 10:03 AM, Oleg Nemanov wrote:

But i can't modify this data in src_proc1/dst_proc1 in place, in this case.

So, how you solve these situations?

Hello,

Instead of upvars, you could use "global ctx" in the proc's.  Then your specialized proc's can work with the full dict or you can send the key names as you need to.


Subject: Re: work with nested struct in proc in TCL
From: Oleg Nemanov
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 14:47 UTC
References: 1 2
X-Received: by 2002:a05:6214:d0b:b0:446:59c7:8390 with SMTP id 11-20020a0562140d0b00b0044659c78390mr3957845qvh.41.1650638822850;
Fri, 22 Apr 2022 07:47:02 -0700 (PDT)
X-Received: by 2002:aca:2b0d:0:b0:322:b685:f583 with SMTP id
i13-20020aca2b0d000000b00322b685f583mr2400190oik.57.1650638822592; Fri, 22
Apr 2022 07:47:02 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!pasdenom.info!usenet-fr.net!fdn.fr!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 07:47:02 -0700 (PDT)
In-Reply-To: <t3ueaa$1noh$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=194.190.114.28; posting-account=RPJNegoAAAAUgD_yLdrci9D1ZtZ1oI0L
NNTP-Posting-Host: 194.190.114.28
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com> <t3ueaa$1noh$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ee015ebd-5372-433f-88d8-6d232fc603ean@googlegroups.com>
Subject: Re: work with nested struct in proc in TCL
From: oleg.o.n...@gmail.com (Oleg Nemanov)
Injection-Date: Fri, 22 Apr 2022 14:47:02 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
View all headers
пятница, 22 апреля 2022 г. в 17:32:46 UTC+3, saito...@gmail.com:
Hello,

Instead of upvars, you could use "global ctx" in the proc's. Then your
specialized proc's can work with the full dict or you can send the key
names as you need to.

I thought about something like this. But operation like 'dict get' where we need '$' before a first argument make this
method harder.


Subject: Re: work with nested struct in proc in TCL
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Organization: Aioe.org NNTP Server
Date: Fri, 22 Apr 2022 14:54 UTC
References: 1 2 3
Path: i2pn2.org!i2pn.org!aioe.org!a5rWVvs5S5ZXUwkNcVnRMw.user.46.165.242.91.POSTED!not-for-mail
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Subject: Re: work with nested struct in proc in TCL
Date: Fri, 22 Apr 2022 10:54:57 -0400
Organization: Aioe.org NNTP Server
Message-ID: <t3ufk2$epg$1@gioia.aioe.org>
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
<t3ueaa$1noh$1@gioia.aioe.org>
<ee015ebd-5372-433f-88d8-6d232fc603ean@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="15152"; posting-host="a5rWVvs5S5ZXUwkNcVnRMw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.6.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
View all headers
On 4/22/22 10:47 AM, Oleg Nemanov wrote:

I thought about something like this. But operation like 'dict get' where we need '$' before a first argument make this
method harder.

Hello,

I am not really clear here but I suspect you need the ability to work with different keys. You can make the key name a parameter to the proc's. You can even parameterize dict name itself if you have multiple ones with a similar structure.



Subject: Re: work with nested struct in proc in TCL
From: Rich
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 22 Apr 2022 15:02 UTC
References: 1
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: work with nested struct in proc in TCL
Date: Fri, 22 Apr 2022 15:02:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <t3ug1s$s5p$1@dont-email.me>
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
Injection-Date: Fri, 22 Apr 2022 15:02:20 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="a61c2cbfb9f5fad2d333ce075f6e2ee4";
logging-data="28857"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19uSWEluFTWaky4M8BeUwez"
User-Agent: tin/2.0.1-20111224 ("Achenvoir") (UNIX) (Linux/3.10.17 (x86_64))
Cancel-Lock: sha1:EK2rvjatIXcqL5FSDa1ozcxvu3I=
View all headers
Oleg Nemanov <oleg.o.nemanov@gmail.com> wrote:
Hi, all.

I have some package where some procs get as argument  a dict with
nested dicts.  These procs call another internal procs that get as
argument this dict too, but needs only a subdict to do its work.  I
can't pass this subdict by name, because i can't modify it in place,
in this case.

I'm somewhat confused by the above.  If you don't want to modify the
subdict, then there is no need to pass it by name.  Just pass in the
subdict.

i.e.:


  set ctx [dict create src [dict create SOME_KEY SOME_DATA] \
                       dst [dict create SOME_KEY SOME_DAT]]

  proc do_work {ctx} {
   src_proc1 [dict get $ctx src]
   dst_proc1 [dict get $ctx dst]
   ...
  }
 
  proc src_proc1 {ctx} {
   # do things with the 'src' sub dict
  }


However, if you in fact 'do' want to modify the subdict, you still do
not /need/ to pass by name, doing so just makes the call site a bit
more complex:

  set ctx [dict create src [dict create SOME_KEY SOME_DATA] \
                       dst [dict create SOME_KEY SOME_DAT]]

  proc do_work {ctx} {
   dict set ctx src [src_proc1 [dict get $ctx src]]
   dict set ctx dst [dst_proc1 [dict get $ctx dst]]
   ...
  }
 
  proc src_proc1 {ctx} {
    dict set ctx NEW_KEY NEW_DATA
    dict set ctx SOME_KEY NEW_DATA
    return $ctx
  }

The runtime cost is a bit more copying and garbage collection, but as
you have omitted any information as to the size of 'ctx', I can't know
whether that cost is excessive or not.


Subject: Re: work with nested struct in proc in TCL
From: Schelte
Newsgroups: comp.lang.tcl
Organization: KPN B.V.
Date: Fri, 22 Apr 2022 15:09 UTC
References: 1
Date: Fri, 22 Apr 2022 17:09:44 +0200
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1
Subject: Re: work with nested struct in proc in TCL
Content-Language: en-US
Newsgroups: comp.lang.tcl
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
From: nos...@wanadoo.nl (Schelte)
In-Reply-To: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <nnd$68e92a13$75d5f877@ef799696278078a5>
Organization: KPN B.V.
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!94.232.112.246.MISMATCH!abe006.abavia.com!abp003.abavia.com!news.kpn.nl!not-for-mail
Lines: 42
Injection-Date: Fri, 22 Apr 2022 17:09:44 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
View all headers
On 22/04/2022 16:03, Oleg Nemanov wrote:
I want to pass to an internal proc just needed subdist like(thus, internal procs will be know only keys its needed for work):

proc do_work {_ctx} {
   upvar $_ctx ctx
   ...
   src_proc1 [dict get $ctx src]
   dst_proc1 [dict get $ctx dst]
   ...
}

But i can't modify this data in src_proc1/dst_proc1 in place, in this case.

So, how you solve these situations?

You can use [dict with] (if src and dst are guaranteed to be present), or [dict update]:

proc sub_proc {_sub} {
   upvar $_sub sub
   dict set sub SOME_KEY 1
}

proc do_work {_ctx} {
   upvar $_ctx ctx
   # ...
   dict update ctx src src dst dst {
     sub_proc src
     sub_proc dst
   }
   # ...
}

set ctx {}
do_work ctx
puts $ctx

Result: src {SOME_KEY 1} dst {SOME_KEY 1}


Schelte


Subject: Re: work with nested struct in proc in TCL
From: Oleg Nemanov
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 16:18 UTC
References: 1 2 3 4
X-Received: by 2002:a37:5e42:0:b0:69a:eac:d843 with SMTP id s63-20020a375e42000000b0069a0eacd843mr3102465qkb.526.1650644290201;
Fri, 22 Apr 2022 09:18:10 -0700 (PDT)
X-Received: by 2002:a9d:6488:0:b0:603:4051:da06 with SMTP id
g8-20020a9d6488000000b006034051da06mr2066348otl.298.1650644289865; Fri, 22
Apr 2022 09:18:09 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 09:18:09 -0700 (PDT)
In-Reply-To: <t3ufk2$epg$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=194.190.114.28; posting-account=RPJNegoAAAAUgD_yLdrci9D1ZtZ1oI0L
NNTP-Posting-Host: 194.190.114.28
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
<t3ueaa$1noh$1@gioia.aioe.org> <ee015ebd-5372-433f-88d8-6d232fc603ean@googlegroups.com>
<t3ufk2$epg$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a679ba7d-07a6-4e69-a054-1c69b39ba893n@googlegroups.com>
Subject: Re: work with nested struct in proc in TCL
From: oleg.o.n...@gmail.com (Oleg Nemanov)
Injection-Date: Fri, 22 Apr 2022 16:18:10 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 28
View all headers
пятница, 22 апреля 2022 г. в 17:55:02 UTC+3, saito...@gmail.com:
On 4/22/22 10:47 AM, Oleg Nemanov wrote:

I thought about something like this. But operation like 'dict get' where we need '$' before a first argument make this
method harder.
Hello,

I am not really clear here but I suspect you need the ability to work
with different keys. You can make the key name a parameter to the
proc's. You can even parameterize dict name itself if you have multiple
ones with a similar structure.

The things is even more complecated if there is nested list with dict like this:
set ctx [dict create srcs [list [dict create SOME_DATA] [dict create SOME_DATA]]]

And we want to process each list item in a loop. In this case, Rich and Schelte
method isn't work and, it seems, the more simple way is:

set src [lindex [dict get $ctx srcs] 0]
src_proc1 src
dict set ctx srcs [lreplace [dict get $ctx srcs] 0 0 $src]

And i thinkg this is not very effectively :-).


Subject: Re: work with nested struct in proc in TCL
From: Oleg Nemanov
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 16:22 UTC
References: 1 2
X-Received: by 2002:ae9:ed87:0:b0:69e:bef0:9b47 with SMTP id c129-20020ae9ed87000000b0069ebef09b47mr3127016qkg.633.1650644520733;
Fri, 22 Apr 2022 09:22:00 -0700 (PDT)
X-Received: by 2002:a05:6808:1244:b0:322:6925:3f4f with SMTP id
o4-20020a056808124400b0032269253f4fmr6904650oiv.39.1650644520520; Fri, 22 Apr
2022 09:22:00 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!nntp.club.cc.cmu.edu!45.76.7.193.MISMATCH!3.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Fri, 22 Apr 2022 09:22:00 -0700 (PDT)
In-Reply-To: <t3ug1s$s5p$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=194.190.114.28; posting-account=RPJNegoAAAAUgD_yLdrci9D1ZtZ1oI0L
NNTP-Posting-Host: 194.190.114.28
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com> <t3ug1s$s5p$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f9033a6e-fb1f-461c-a9be-f032b5ef365cn@googlegroups.com>
Subject: Re: work with nested struct in proc in TCL
From: oleg.o.n...@gmail.com (Oleg Nemanov)
Injection-Date: Fri, 22 Apr 2022 16:22:00 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 9
View all headers
пятница, 22 апреля 2022 г. в 18:02:24 UTC+3, Rich:
The runtime cost is a bit more copying and garbage collection, but as
you have omitted any information as to the size of 'ctx', I can't know
whether that cost is excessive or not.

In my case a size of ctx is small. Just 2 nested dicts with few keys in each.
But this case prompted me to think about more general cases and how others
solve this :-). And also what to do if a size of dict is big.


Subject: Re: work with nested struct in proc in TCL
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Organization: Aioe.org NNTP Server
Date: Fri, 22 Apr 2022 18:24 UTC
References: 1 2 3 4 5
Path: i2pn2.org!i2pn.org!aioe.org!a5rWVvs5S5ZXUwkNcVnRMw.user.46.165.242.91.POSTED!not-for-mail
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Subject: Re: work with nested struct in proc in TCL
Date: Fri, 22 Apr 2022 14:24:39 -0400
Organization: Aioe.org NNTP Server
Message-ID: <t3urt8$bnl$1@gioia.aioe.org>
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
<t3ueaa$1noh$1@gioia.aioe.org>
<ee015ebd-5372-433f-88d8-6d232fc603ean@googlegroups.com>
<t3ufk2$epg$1@gioia.aioe.org>
<a679ba7d-07a6-4e69-a054-1c69b39ba893n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="12021"; posting-host="a5rWVvs5S5ZXUwkNcVnRMw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.6.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
View all headers
On 4/22/22 12:18 PM, Oleg Nemanov wrote:

The things is even more complecated if there is nested list with dict like this:
set ctx [dict create srcs [list [dict create SOME_DATA] [dict create SOME_DATA]]]

And we want to process each list item in a loop. In this case, Rich and Schelte
method isn't work and, it seems, the more simple way is:


Hello,

Your explanation could use some help.  In any case, it looks like using global would solve your dilemma:

set ctx [dict create src {10 20 30} dst {100 200 300}]

proc add_one {key} {
global ctx

# process it. see note at the end.
set new_x {}
foreach x [dict get $ctx $key] {
  lappend new_x [expr $x + $x]
}

dict set ctx $key $new_x
}

add_one src
add_one src
add_one dst


So now you can individually process the keys. You can parameterize the computation too.





Subject: Re: work with nested struct in proc in TCL
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Organization: Aioe.org NNTP Server
Date: Fri, 22 Apr 2022 18:50 UTC
References: 1 2 3 4 5 6
Path: i2pn2.org!i2pn.org!aioe.org!a5rWVvs5S5ZXUwkNcVnRMw.user.46.165.242.91.POSTED!not-for-mail
From: saitolo...@gmail.com
Newsgroups: comp.lang.tcl
Subject: Re: work with nested struct in proc in TCL
Date: Fri, 22 Apr 2022 14:50:49 -0400
Organization: Aioe.org NNTP Server
Message-ID: <t3utea$1084$1@gioia.aioe.org>
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
<t3ueaa$1noh$1@gioia.aioe.org>
<ee015ebd-5372-433f-88d8-6d232fc603ean@googlegroups.com>
<t3ufk2$epg$1@gioia.aioe.org>
<a679ba7d-07a6-4e69-a054-1c69b39ba893n@googlegroups.com>
<t3urt8$bnl$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="33028"; posting-host="a5rWVvs5S5ZXUwkNcVnRMw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.6.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
View all headers
On 4/22/22 2:24 PM, saitology9@gmail.com wrote:
So now you can individually process the keys. You can parameterize the computation too.



Hello,
Here is an expanded version. You can work with multiple dict's and keys, and apply different proc's to each.


set ctx [dict create src {10 20 30} dst {100 200 300}]
set ctx_2 [dict create src {10 20 30 40 50} dst {100 200 300 400 500}]

proc flexible {dname key pname} {
global ctx
global ctx_2

# process it.
dict set $dname $key [$pname [dict get [set $dname] $key]]

}

proc add {l} {
set new_l {}
foreach x $l {
lappend new_l [expr $x + 1]
}
return $new_l
}

proc multiply {l} {
set new_l {}
foreach x $l {
lappend new_l [expr $x * 2]
}
return $new_l
}


# You can now test it:

flexible ctx src add
flexible ctx src multiply
flexible ctx dst  add


flexible ctx_2 src add
flexible ctx_2 src multiply
flexible ctx_2 dst add



Subject: Re: work with nested struct in proc in TCL
From: Rich
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 22 Apr 2022 18:53 UTC
References: 1 2 3 4 5
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: work with nested struct in proc in TCL
Date: Fri, 22 Apr 2022 18:53:03 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <t3utif$cur$1@dont-email.me>
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com> <t3ueaa$1noh$1@gioia.aioe.org> <ee015ebd-5372-433f-88d8-6d232fc603ean@googlegroups.com> <t3ufk2$epg$1@gioia.aioe.org> <a679ba7d-07a6-4e69-a054-1c69b39ba893n@googlegroups.com>
Injection-Date: Fri, 22 Apr 2022 18:53:03 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="a61c2cbfb9f5fad2d333ce075f6e2ee4";
logging-data="13275"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gc9K7W23OukzR8f60TBnY"
User-Agent: tin/2.0.1-20111224 ("Achenvoir") (UNIX) (Linux/3.10.17 (x86_64))
Cancel-Lock: sha1:GaRWpBlosqerzhPBbfIrHS/lCEU=
View all headers
Oleg Nemanov <oleg.o.nemanov@gmail.com> wrote:
???????, 22 ?????? 2022 ?. ? 17:55:02 UTC+3, saito...@gmail.com:
The things is even more complecated if there is nested list with dict
like this: set ctx [dict create srcs [list [dict create SOME_DATA]
[dict create SOME_DATA]]]

And we want to process each list item in a loop.  In this case, Rich
and Schelte method isn't work and, it seems, the more simple way is:

set src [lindex [dict get $ctx srcs] 0]
src_proc1 src
dict set ctx srcs [lreplace [dict get $ctx srcs] 0 0 $src]

And i thinkg this is not very effectively :-).

Tcl's [time] command can be a great way to see if something seems not
efficient.

In this case, for the set src and src_proc1 src lines, only an
additional pointer, to the list element, should be created.  Should
src_proc1 modify (via upvar) the contents of src, then a copy of what
is in 'src' will be made.

The last line should then replace just the one list element (not copy
and GC the entire dict).  So while some copying and GC will happen, it
should be limited to just the parts that got changed, not the entire
'ctx' dict.

The tcl::unsupported::representation can be very illuminating for
discovering how the runtime deals with manipulating data under the
hood.

Note:

    $ rlwrap tclsh
    % set ctx [dict create srcs [list [dict create a b] [dict create c d]] dst [dict create e f]]
    srcs {{a b} {c d}} dst {e f}
    % tcl::unsupported::representation $ctx
    value is a dict with a refcount of 2, object pointer at 0x115b780,
    internal representation 0x11967e0:(nil), string representation "srcs
    {{a b} {..."
    % tcl::unsupported::representation [lindex [dict get $ctx srcs] 0]
    value is a dict with a refcount of 4, object pointer at 0x1156a70,
    internal representation 0x113df80:(nil), string representation "a b"
    % set src [lindex [dict get $ctx srcs] 0]
    a b
    % tcl::unsupported::representation $src
    value is a dict with a refcount of 5, object pointer at 0x1156a70,
    internal representation 0x113df80:(nil), string representation "a b"
    %

src is now pointing to the same object that five other references are
also pointing towards.

Now, modify via lreplace:

    % set new [dict create y z]
    y z
    % tcl::unsupported::representation $new
    value is a dict with a refcount of 4, object pointer at 0x115b000, internal repr                                                        esentation 0x1196de0:(nil), string representation "y z"
    % dict set ctx srcs [lreplace [dict get $ctx srcs] 0 0 $new]
    srcs {{y z} {c d}} dst {e f}
    % tcl::unsupported::representation $ctx
    value is a dict with a refcount of 2, object pointer at 0x115b780, internal repr                                                        esentation 0x11967e0:(nil), string representation "srcs {{y z} {..."
    % tcl::unsupported::representation $new
    value is a dict with a refcount of 5, object pointer at 0x115b000,
    internal representation 0x1196de0:(nil), string representation "y z"
    % tcl::unsupported::representation [lindex [dict get $ctx srcs] 0]
    value is a dict with a refcount of 5, object pointer at 0x115b000,
    internal representation 0x1196de0:(nil), string representation "y z"
    %

ctx is the same object pointer as before (0x115b780).

the new dict began life as poinnter at 0x115b000, and remained the same
after 'splicing'

The item spliced in is the same obj pointer as new when it began life
(0x115b000).  So the thing that was in 'new' just got 'spliced into'
the list by having the first list cell point at the same thing "new"
was pointing at.



Subject: Re: work with nested struct in proc in TCL
From: Oleg Nemanov
Newsgroups: comp.lang.tcl
Date: Tue, 26 Apr 2022 14:25 UTC
References: 1 2 3 4 5 6
X-Received: by 2002:a05:6214:4104:b0:42c:1db0:da28 with SMTP id kc4-20020a056214410400b0042c1db0da28mr16372777qvb.67.1650983139532;
Tue, 26 Apr 2022 07:25:39 -0700 (PDT)
X-Received: by 2002:a05:6870:d254:b0:db:12b5:da3 with SMTP id
h20-20020a056870d25400b000db12b50da3mr13669670oac.211.1650983138910; Tue, 26
Apr 2022 07:25:38 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Tue, 26 Apr 2022 07:25:38 -0700 (PDT)
In-Reply-To: <t3utif$cur$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=194.190.114.28; posting-account=RPJNegoAAAAUgD_yLdrci9D1ZtZ1oI0L
NNTP-Posting-Host: 194.190.114.28
References: <4556aeda-d577-4bf3-8fd2-e032eb996870n@googlegroups.com>
<t3ueaa$1noh$1@gioia.aioe.org> <ee015ebd-5372-433f-88d8-6d232fc603ean@googlegroups.com>
<t3ufk2$epg$1@gioia.aioe.org> <a679ba7d-07a6-4e69-a054-1c69b39ba893n@googlegroups.com>
<t3utif$cur$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ba5ec430-fde0-47e8-a997-fb6ad286cb42n@googlegroups.com>
Subject: Re: work with nested struct in proc in TCL
From: oleg.o.n...@gmail.com (Oleg Nemanov)
Injection-Date: Tue, 26 Apr 2022 14:25:39 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 14
View all headers
пятница, 22 апреля 2022 г. в 21:53:07 UTC+3, Rich:
Tcl's [time] command can be a great way to see if something seems not
efficient.

To see if something seems not efficient we need an another implementation
to compare with :-). [time] by itself is useless in a case where we can't compare with
something. My question was more about how others are solving this large nested structure issue.
To compare with my method :-).

[CUT]

Thanks for the detailed explanation with examples.


1
rocksolid light 0.7.2
clearneti2ptor