Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to use the tablematrix with ‘extended’ selection and get the data of the selected rows.
I do not mind exactly what the data is as long as this lets me identify the rows.
The following comes from the information about a tablemaxtrix.
“The selection
Table selections are available as type STRING. By default, the value of the selection will be the
values of the selected cells in nested Tcl list form where each row is a list and each column is
an element of a row list.”
Is $table->curselection(?value?) (description - With no arguments, it returns the sorted
indices of the currently selected cells. Otherwise it sets all the selected cells to the
given value. The set has no effect if there is no associated Tcl array or the state is disabled.)
the correct way of getting the selection as described in “the selection”?
If it is can someone explain how to ‘read’ the indices?
Below is an example of a talbematrix that I found by searching the Monks site. I thought that to may help to give an explanation
#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::TableMatrix; use Tk::TableMatrix::Spreadsheet; my $top = MainWindow->new; my $arrayVar = {}; print "Filling Array...\n"; my ($rows,$cols) = (40000, 10); foreach my $row (0..($rows-1)){ $arrayVar->{"$row,0"} = "$row"; } foreach my $col (0..($cols-1)){ $arrayVar->{"0,$col"} = "$col"; } print "Creating Table...\n"; sub colSub{ my $col = shift; return "OddCol" if( $col > 0 && $col%2) ; } my $label = $top->Label(-text => "TableMatrix v2 Example") ->pack( -expand => 1, -fill => 'both'); my $t = $top->Scrolled('Spreadsheet', -rows => $rows, -cols => $cols, -width => 6, -height => 12, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -colstretchmode => 'last', -flashmode => 1, -flashtime => 2, -wrap=>1, -rowstretchmode => 'last', -selectmode => 'extended', -selecttype=>'cell', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se', -sparsearray=>0 )->pack(-expand => 1, -fill => 'both'); #my $realmatrix = $t->Subwidget('scrolled'); $top->Button( -text => "Clear", -command => sub{&clear})->pack(-expand => 1, -fill => 'x'); $top->Button( -text => "Fill", -command => sub{&fill})->pack(-expand => 1, -fill => 'x'); $top->Button( -text => "Exit", -command => sub{$top->destroy})->pack(-expand => 1, -fill => 'x' +); $t->colWidth( -2 => 8, -1 => 9, 0=> 12, 4=> 14); $arrayVar->{"1,1"} = 42; Tk::MainLoop; ######################################### sub TMRefresh { #Required input TableMatrix object. #use to force matrix to update, a code trick return if (!$_[0]); $_[0]->configure(-padx =>($_[0]->cget(-padx))); #$realmatrix->update; #$t->update; #$top->update; #$t->see("100,100"); #trick to force update? #$t->see("end"); #$t->see("1,1"); } ####################################### sub clear{ #$t->clearAll('0,0','end'); foreach my $row(1..$rows){ foreach my $col(1..$cols){ $arrayVar->{"$row,$col"} = 0; } } &TMRefresh($t); } ##################################### sub fill{ foreach my $row(1..$rows){ foreach my $col(1..$cols){ $arrayVar->{"$row,$col"} = 1000; } } &TMRefresh($t); } #######################################

Replies are listed 'Best First'.
Re: Tk Tablematrix extended selection data
by lamprecht (Friar) on Jun 05, 2011 at 18:18 UTC
    ~$ perl -MTk -MData::Dumper -e' $m = tkinit->TableMatrix(-selectmode=>"extended",-variable=>{})->pack; $m->bind("<3>",sub{print Dumper [$m->get(($m->curselection)[0,-1])]}); MainLoop'

    use curselection() for a list of index strings, use get(first,last) to get the values.


    Cheers, Chris
      Great just what I wanted!
      Many thanks