in reply to Re^3: A TableMatrix row selection question
in thread A TableMatrix row selection question

Hello Zentara,

Thank you for the quick response. Yes it is true that working with an abstract understanding helps many times. Therefore I have no problems accepting some things as internal magic so long as I understand what values and in what format I need to pass when calling these subroutines. For example, in the earlier mentioned thread: http://perlmonks.com/?node_id=840707 (i) what values and in what format I pass to select a column and be able to retrieve the column number that was selected (ii) similarly how to select an individual cell and retrieve the row,col of that cell.

But without even knowing what values and in what format to pass, I'm having difficulty even for abstract understanding at the programmer level. Yes, I did earlier try commenting out each of these three calls but the program fails to work. I'm taking a look at the HList selection example thread you mentioned. I hope it helps.

Meanwhile if somebody can tell me what values (and format) I need to pass to these three subroutines to get column selection, individual cell selection etc., it will be whole lot useful. Also, what does the @{...} syntax do ?? (the one used while calling curselection sub)

Thanks again
  • Comment on Re^4: A TableMatrix row selection question

Replies are listed 'Best First'.
Re^5: A TableMatrix row selection question
by zentara (Cardinal) on Aug 31, 2014 at 09:05 UTC
    Also, what does the @{...} syntax do

    Besides confuse you? heh heh . Seriously, I'm not the expert on it, but it is an array dereference. The part in brackets {..} represent an hashref ( a hash reference), which is how stuff is stored in an object. The @ part tells it that the hashref is an array, and needs an array to put it in for display. Google for Perl Dereferencing for alot of tutorials and explanations.

    As to your other question, how to get the n'th column from any row selected in the HList widget, this recently posted script shows the way.

    #!/usr/bin/perl use Tk; use Tk::HList; use YAML; $top = new MainWindow; $hlist = $top->Scrolled("HList", -header => 1, -columns => 4, -scrollbars => 'osoe', -command => sub{print 1}, -width => 70, -selectbackground => 'SeaGreen3', -browsecmd => \&browseThis, )->pack(-expand => 1, -fill => 'both'); $hlist->header('create', 0, -text => 'From'); $hlist->header('create', 1, -text => 'Subject'); $hlist->header('create', 2, -text => 'Date'); $hlist->header('create', 3, -text => 'Size'); $hlist->add(0); $hlist->itemCreate(0, 0, -text => "eserte\@cs.tu-berlin.de"); $hlist->itemCreate(0, 1, -text => "Re: HList?"); $hlist->itemCreate(0, 2, -text => "1999-11-20"); $hlist->itemCreate(0, 3, -text => "1432"); $hlist->add(1); $hlist->itemCreate(1, 0, -text => "dummy\@foo.com"); $hlist->itemCreate(1, 1, -text => "Re: HList?"); $hlist->itemCreate(1, 2, -text => "1999-11-21"); $hlist->itemCreate(1, 3, -text => "2335"); MainLoop; sub browseThis{ for my $column (0..3){ print $hlist->itemCget( $hlist->selectionGet, $column, 'text' ) +, "\n"; } } =head1 my $listArrayRef = []; my @selectedindices = $hlist->info('selection'); foreach my $r (@selectedindices) { push @{$listArrayRef}, getRowArrayRef($hlist, $r); print Dump(@{$listArrayRef}),"\n"; } sub getRowArrayRef { my ($hlist, $r) = @_; my @row; foreach my $c (0 .. $hlist->cget(-columns) -1) { push @row, $hlist->itemCget($r, $c, '-text'); } return \@row; } } =cut

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Alright. Sorry for getting back to this little late. I again had some problems with my internet connection and had to get the service provider folks to test the modem for me. Now it has been set right (hopefully). Thereafter studying through the responses and trying out examples also took some time. However, below I have summarized what I've learnt from this thread so that any newbie like me will find it useful. My thanks to stefbv and anonymous monk for swiftly responding to which was essentially a four year old thread. My special thanks to zentara for successive follow-up responses and detailed examples. I've one question though which I've posted in the end.


      What was learnt:

      1. perldoc Tk::bind under section: "BINDING CALLBACKS AND SUBSTITUTIONS" lists all the fields that are accessible to a Perl/Tk programmer from XEvent object

      2. perldoc Tk::TableMatrix under section: "Indices" lists all the formats of the paramaters to the widget commands such as selectionIncludes, selectionSet. The last format listed is: @x,y shows that @ is simply "at" but nothing to do with array data type.

      3. The Tk::XEvent object is generic to any widget but not specific to Tk::TableMatrix. Therefore whenever mouse moves over a tablematrix widget (i.e. <Motion> event), the event object cannot specify on which row, column or cell the mouse is at but only the (x,y) co-ordinates in the widget where the mouse is at. This causes the methods selectionIncludes and selectionSet to use the format: @x,y by accessing x and y co-ordinate values from XEvent object

      4. The selectionIncludes and selectionSet are cell specific i.e. the selection pertains to a cell covering the (x,y) co-ordinates. However, the option 'selectype' (which by default is set to 'cell') when set to 'row' causes the selectionSet to select the whole row

      5. The 'curselection' method returns the sorted indices of all the cells in the selected row in the format: x,y for example: when 2nd row is selected in a table of 2-by-3 matrix, the first element of the array has value 1,0 and last element being of value 1,2 and the length of this array is 3. print ref w->curselection outputs: ARRAY which means curselection method returns a reference to an array but not the array itself. Therefore it has be de-referenced via the construct: @{...}

      6. perldoc Tk::TableMatrix under section: "Tags" lists all the built-in tags. The selected row and it's content can be highlighted, if needed, using the 'sel' tag by
      <I> my $tmatrix_select_font = [-family => 'Linotype Birka', -size => 15, -weight => 'bold', -slant => 'roman', -underline => 1, -overstrike => 0 ]; $w->tagConfigure('sel', -bg=>'cyan', -fg=>'blue', -font=>$tmatrix_ +select_font); </I>

      One question I've though is
      if the w->curselection method returns a reference to an array (as seen from: ref w->curselection;) then how come:
      $,='|'; print w->curselection;
      prints the contents of the array.??? i.e. the contents of the array are flattened into a string and printed instead of dumping the reference (something like: ARRAY(0x38cfc) i.e. what happens when the reference is used as a string).

      Any answers ???

      Many Thanks

        One question I've though is
        if the w->curselection method returns a reference to an array (as seen from: ref w->curselection;) then how come:

        $,='|'; print w->curselection;

        prints the contents of the array.??? i.e. the contents of the array are flattened into a string and printed instead of dumping the reference (something like: ARRAY(0x38cfc) i.e. what happens when the reference is used as a string).

        That method probably uses wantarray to figure out whether it's called in list or scalar context, and returns an array or array reference, as appropriate. Since print and friends provide list context, you get the array itself in your snippet.

        Here's a simple demonstration of this behavior:

        #!/usr/bin/perl use strict; use warnings; use feature qw/say/; sub test { my @list = (1, 2, 3); return wantarray ? @list : \@list; } $, = ","; say test(); my $ref = test(); say $ref;

        This prints e.g.:

        $ perl 1099646.pl 1,2,3 ARRAY(0x80071c80) $