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

Dear Fellow monks,

I seek your wisdom on a particular issue with the Tk::Columns module. I have a large text file that i need to display in a tabular form and sort. I discovered this Tk::Columns module and I found it to be up to the task. However, I have two basic problems (both of which are closely related ) regarding the configuration of the table . First the code (with some sample data)

#! /usr/bin/perl use Tk; use strict; use warnings; use Tk::Columns; my $l_MainWindow = Tk::MainWindow->new(); $l_MainWindow->geometry("800x500+0+0"); my $l_Columns = $l_MainWindow->Columns ( '-command' => sub {printf ("Selected [%s]\n", join ('|', @_));}, '-image' => $l_MainWindow->Pixmap ('-file' => 'mini-doc.xpm'), '-listforeground' => 'blue4', '-listbackground' => 'beige', '-buttonbackground' => 'brown', '-buttonforeground' => 'white', '-selectmode' => 'extended', '-sort' => 'true', )->pack ( '-expand' => 'true', '-fill' => 'both', ); my @columns_desc = ("Col A", "Col B", "Col C", "Col D", "Col E +" ); $l_Columns->ColumnButton( -text => "$columns_desc[0]", -width => 10 ); $l_Columns->ColumnButton( -text => "$columns_desc[1]", -width => 10 ); $l_Columns->ColumnButton( -text => "$columns_desc[2]", -width => 10 ); $l_Columns->ColumnButton( -text => "$columns_desc[3]", -width => 10 ); + $l_Columns->ColumnButton( -text => "$columns_desc[4]", -width => 10 ); + while (<DATA>) { chomp; $_ =~ s/[\r\n]*$//; $l_Columns->insert ('end', split()); } Tk::MainLoop(); __DATA__ drwxrwxrwx 2 nobody nogroup 512 May 1 19:20 . drwxrwxrwx 5 nobody nogroup 512 Apr 30 01:14 .. -rwxrwxrwx 1 dkw nogroup 3774 Dec 31 1969 camel.ico -rwxrwxrwx 1 dkw nogroup 1888 Apr 29 15:19 camel_s.gif -rwxrwxrwx 1 dkw nogroup 218 Apr 8 12:11 checkbox.pl -rwxrwxrwx 1 dkw nogroup 8272 Apr 8 12:11 client.xpm -rwxrwxrwx 1 dkw nogroup 538 May 1 19:23 columns.pl -rwxrwxrwx 1 dkw nogroup 655 Apr 8 12:11 comboentry.pl -rwxrwxrwx 1 dkw nogroup 2873 Apr 8 12:11 database.xpm -rwxrwxrwx 1 dkw nogroup 3205 Apr 8 12:11 iconcanvas.pl -rwxrwxrwx 1 dkw nogroup 2711 Apr 8 12:11 mail.xpm -rwxr--r-- 1 nobody nogroup 837 May 1 19:00 menustrip.pl -rwxrwxrwx 1 dkw nogroup 8272 Apr 8 12:11 netclient.xpm -rwxrwxrwx 1 dkw nogroup 587 Apr 8 12:11 progressindicato +r.pl -rwxrwxrwx 1 dkw nogroup 7879 Apr 8 12:11 server.xpm -rwxrwxrwx 1 dkw nogroup 982 Apr 8 12:11 splitframe.pl -rwxrwxrwx 1 dkw nogroup 919 Apr 8 12:11 tabbedform.pl -rwxrwxrwx 1 dkw nogroup 2020 Apr 29 12:44 tabframe.pl -rwxrwxrwx 1 dkw nogroup 1239 Apr 8 12:11 tableedit.pl -rwxrwxrwx 1 dkw nogroup 156 Apr 8 12:11 test.dat -rwxrwxrwx 1 dkw nogroup 0 May 1 19:24 testfile.txt

Now, the problems :

1. How do I reconfigure the height of the rows ?. The text file is very large. Since the data is placed close to each other, the data looks crowded and messy. I want to increase the spacing to make it look much cleaner

2. How do i insert a new line character in one of the table entries.? Now, I have multiline entries in some rows e.g : "This is a \n sample \n text" (although I have not included such entries here). The moment it encounters such a value, it seems to trim out the new line characters and proceeds with printing the entry. HOw do i fix this ??

Thank you in advance..!

Replies are listed 'Best First'.
Re: Tk::Columns height and new line character
by zentara (Cardinal) on Jan 24, 2014 at 17:59 UTC
    Try adding the -width option to your Columns object, like -width => 50. Experiment around at look thru the examples and docs. You can also use the old trick of adding spaces to the heads and tails of the column header names. :-)

    Right now, I can't understand what your \n problem is. Can you provide an example.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Tk::Columns height and new line character
by reaper9187 (Scribe) on Jan 24, 2014 at 23:51 UTC
    Thank you for your reply zentara

    The problem is actually easy to reproduce. Consider appending the following lines at the end of the code
    $l_Columns->insert('end', { 'Col A' => "Sample Text A", 'Col B' => "This is a \n sample \n text ", 'Col C' =>"Sample Text \n C", 'Col D' =>"Sample Text \n D", 'Col E' =>"Sample Text \n E", + } );
    There are several such entries in the file.

    The observation : The newline character ( "\n" ) is removed and a multiline entry is printed as a single line which makes the entire data look too crowded. I have tried different options (assigning the string to a variable, arrays ,etc still it doesn't seem to work).
    I really like this module because of the options it provides ( clean output, sorting ,etc) . I'd really like to find a soln to this problem bcoz it gets really messy when you try and piece together different modules to achieve the desired tabular display with sorting etc.
    Help greatly appreciated.