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

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

Replies are listed 'Best First'.
Re^7: A TableMatrix row selection question
by AppleFritter (Vicar) on Sep 05, 2014 at 09:42 UTC

    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) $
      It can also use overload:
      #!/usr/bin/perl use warnings; use strict; { package My; use overload '""' => sub { "@{ $_[0] }" }; sub new { my $class = shift; bless [ @_ ], $class } } my $ar = 'My'->new(2, 3, 4, 5, 7); print ref $ar ? "Reference\n" : "Not a reference\n"; print $ar, "\n";

      Update: ref $ar returns "My" in this case, not "ARRAY". So it can't be the case here, unless the class is named ARRAY.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ