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

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

Replies are listed 'Best First'.
Re^6: A TableMatrix row selection question
by Anonymous Monk on Sep 05, 2014 at 06:33 UTC

    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) $
        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.

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