Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Checkbutton in a loop

by Eliya (Vicar)
on Apr 29, 2012 at 08:37 UTC ( #967918=note: print w/replies, xml ) Need Help??

in reply to Checkbutton in a loop

What do you expect $j to be when the callback function gets called?  You have to somehow pass it as an argument to the routine.  There are two ways to pass the value:

Either specify an array(ref) to the -command option

... for my $i (0...$#a) { $f->Checkbutton(-text => $a[$i], -command => [ \&fun, $i ] )->pack +(); } MainLoop; sub fun { my $j = shift; print $a[$j]; }

Or use a closure

... for my $i (0...$#a) { $f->Checkbutton(-text => $a[$i], -command => sub { fun($i) } )->pa +ck(); } ...

Replies are listed 'Best First'.
Re^2: Checkbutton in a loop
by Anonymous Monk on Apr 29, 2012 at 09:27 UTC

    There is a third option, don't pass it any arguments

    use Tk; $mw = tkinit; for(1..4){ $mw->Checkbutton( -text => qq/check it $_/, -command => \&f, ) ->pack; } MainLoop; sub f { my $b = $Tk::event->W; warn join q/ /, $b->{Value}, $b->cget(q/-text/); } __END__ 1 check it 3 at - line 14. 0 check it 3 at - line 14. 1 check it 3 at - line 14. 0 check it 3 at - line 14. 1 check it 3 at - line 14. 0 check it 3 at - line 14. 1 check it 3 at - line 14. 0 check it 3 at - line 14.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967918]
[Corion]: Of course, the query performance for "all items on page 10" is likely worse than rank() between 100 and 109 , but if that means I can write 15 lines of SQL instead of needing to think about how to partition things and how to encode the page size...
[Corion]: ... that would be nice. But alas, I'm currently tied to SQLite as minimum implementation, and it doesn't implement window functions :-(
[Corion]: And I'm not aware of any other serverless SQL implementation that even reaches the capability of SQLite, not to mention surpasses it

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2018-03-22 12:21 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (274 votes). Check out past polls.