Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Almost nothing in Perl serves a single purpose. -- Larry Wall in <199712040054.QAA13811@wall.org>


devel / comp.lang.tcl / Flummoxed by foreach behavior

SubjectAuthor
* Flummoxed by foreach behaviorLuc
+- Flummoxed by foreach behaviorHarald Oehlmann
`- Flummoxed by foreach behaviorRalf Fassel

1
Flummoxed by foreach behavior

<20221025021353.5ee4d0d4@lud1.home>

  copy mid

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

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!aioe.org!XFiugZAP+2VeIdFIT80Emw.user.46.165.242.75.POSTED!not-for-mail
From: no...@no.no (Luc)
Newsgroups: comp.lang.tcl
Subject: Flummoxed by foreach behavior
Date: Tue, 25 Oct 2022 02:13:53 -0300
Organization: Aioe.org NNTP Server
Message-ID: <20221025021353.5ee4d0d4@lud1.home>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="58124"; posting-host="XFiugZAP+2VeIdFIT80Emw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Newsreader: Claws Mail 3.14.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu)
X-Notice: Filtered by postfilter v. 0.9.2
 by: Luc - Tue, 25 Oct 2022 05:13 UTC

bind $::widget <Key-1> {set ::widget::choice $i; puts $::widget::choice}
bind $::widget <Key-2> {set ::widget::choice $i; puts $::widget::choice}
bind $::widget <Key-3> {set ::widget::choice $i; puts $::widget::choice}
bind $::widget <Key-4> {set ::widget::choice $i; puts $::widget::choice}

This works fine.
I press 1, the app outputs 1. I press 2, the app outputs 2.
And so on.

However,

foreach i {1 2 3 4 5 6 7 8 9 0} {
bind $::widget <Key-$i> {set ::widget::choice $i; puts $::widget::choice}
}

I press 1, the app outputs 0. I press 2, the app outputs 0.
I press 3, the app outputs 0.
And so on.

The output always is the last item in the list. I tested that.

What in the blue blazes? I am completely stumped.

TIA

--
Luc
>>

Re: Flummoxed by foreach behavior

<tj878a$1v0qk$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: wortka...@yahoo.com (Harald Oehlmann)
Newsgroups: comp.lang.tcl
Subject: Re: Flummoxed by foreach behavior
Date: Tue, 25 Oct 2022 10:37:31 +0200
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <tj878a$1v0qk$1@dont-email.me>
References: <20221025021353.5ee4d0d4@lud1.home>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 25 Oct 2022 08:37:30 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="484796883f9d271dac05ff063d89fde7";
logging-data="2065236"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cP/O/Pzyl8gPMQ9ZRowBn"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.0
Cancel-Lock: sha1:CEsPWdJ8sa1doDOow3r9Y2IGL7k=
Content-Language: en-GB
In-Reply-To: <20221025021353.5ee4d0d4@lud1.home>
 by: Harald Oehlmann - Tue, 25 Oct 2022 08:37 UTC

Am 25.10.2022 um 07:13 schrieb Luc:
> bind $::widget <Key-1> {set ::widget::choice $i; puts $::widget::choice}
> bind $::widget <Key-2> {set ::widget::choice $i; puts $::widget::choice}
> bind $::widget <Key-3> {set ::widget::choice $i; puts $::widget::choice}
> bind $::widget <Key-4> {set ::widget::choice $i; puts $::widget::choice}
>
> This works fine.
> I press 1, the app outputs 1. I press 2, the app outputs 2.
> And so on.
>
> However,
>
> foreach i {1 2 3 4 5 6 7 8 9 0} {
> bind $::widget <Key-$i> {set ::widget::choice $i; puts $::widget::choice}
> }
>
> I press 1, the app outputs 0. I press 2, the app outputs 0.
> I press 3, the app outputs 0.
> And so on.
>
> The output always is the last item in the list. I tested that.
>
> What in the blue blazes? I am completely stumped.
>
> TIA
>

What about this :

foreach i {1 2 3 4 5 6 7 8 9 0} {
bind $::widget <Key-$i> "set ::widget::choice $i; puts
\$::widget::choice"
}

Re: Flummoxed by foreach behavior

<ygasfjcqkts.fsf@akutech.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralf...@gmx.de (Ralf Fassel)
Newsgroups: comp.lang.tcl
Subject: Re: Flummoxed by foreach behavior
Date: Tue, 25 Oct 2022 12:06:07 +0200
Lines: 77
Message-ID: <ygasfjcqkts.fsf@akutech.de>
References: <20221025021353.5ee4d0d4@lud1.home>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net HrxAmZyGjcvAZKRJMOSsggWLbLWF368DGq0iW/etrQAkkJzjY=
Cancel-Lock: sha1:Zg9LD4TKEAJzJnd3dwfkbuW3HpE= sha1:XGLYS9AujlkHv6Ieg8e9dsVov2Y=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
 by: Ralf Fassel - Tue, 25 Oct 2022 10:06 UTC

* Luc <no@no.no>
| bind $::widget <Key-1> {set ::widget::choice $i; puts $::widget::choice}
| bind $::widget <Key-2> {set ::widget::choice $i; puts $::widget::choice}
| bind $::widget <Key-3> {set ::widget::choice $i; puts $::widget::choice}
| bind $::widget <Key-4> {set ::widget::choice $i; puts $::widget::choice}
>
| This works fine.
| I press 1, the app outputs 1. I press 2, the app outputs 2.

I don't believe that.

$ wish
% set widget [label .l -text "Click on me"]
% pack $widget
% focus $widget
% bind $::widget <Key-1> {set ::widget::choice $i; puts $::widget::choice}
now press "1"
=> error: so such variable: i

| foreach i {1 2 3 4 5 6 7 8 9 0} {
| bind $::widget <Key-$i> {set ::widget::choice $i; puts $::widget::choice}
| }
>
| I press 1, the app outputs 0. I press 2, the app outputs 0.
| I press 3, the app outputs 0.
| And so on.

You need to understand what the 'bind' command is doing: it sets up a
command to run later when the Event occurs.
Now in your case, since the command is enclosed in {}, there is no
variable substitution at the bind call.
Rather the substitution takes place later when the event occurs.
Confirm:

% bind $::widget <Key-1>
=> set ::widget::choice $i; puts $::widget::choice
% bind $::widget <Key-2>
=> set ::widget::choice $i; puts $::widget::choice
...

Note how the $i is still there in all cases, it was not replaced when
the bind was set up. Since in my test there was no variable 'i', I get
the error. In your test, the loop variable 'i' was last set to 0, so
this is what you see in all cases.

Solutions:

=> Check the available %-replacements for bind (most likely %K)

=> use 'list' to set up the binding if the variables should get resolved
when the bind is set:
bind ... "[list set ::widget::choice $i] ; puts \$::widget::choice"

=> for anything more substantial than a 'puts', use a helper proc to
call when the event happens:

proc some_action {key} {
switch -- $key {
1 - 2 - 3 - 4 {
puts "key $key"
}
5 {
puts "nononono!"
}
default {
puts "don't know what to do with $key"
}
}
}
foreach k {1 2 3 4 5} {
bind $widget <Key-$k> [list some_action %K]
}
# or even
bind $widget <KeyPress> [list some_action %K]

HTH
R'

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor