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

Hi friendly folks! I'm writing a small app for a friend in Gtk2(Glade3) and Perl to proccess new quotes for his company, as I have little experience in Gtk2 I've hit a pothole and now seek wisdom from you! Right, so I have a textview in my gui, along with a bunch of text entries, someone inputs text into the entries, and with the click of a button they get added to the textview, the problem now is the formatting of the textview looks a bit moot, and I was wondering how to format the textview like a printf type, so that the spaces between the values looks better. An example:
# ID DESCRITPION QTY PRICE 1 los LABOUR ON PARTS 1 760.00 2 sa STRIP 1 450.00 3 av ANOTHER VALUE HERE 1 2000.00
The current code I use to insert the text into the textview is:
$buffer_1->insert($buffer_1->get_end_iter(),"$linenum $kode\t$waarde\t\t\t\t$entry_qty\t\t\t\tR$entry_price\n");
That is sort off what it currently looks like in the textview (I simply seperated the values with tabs), I need everything to be inserted with the same ammount of spaces between, like:
# ID DESCRITPION QTY PRICE 1 los LABOUR ON PARTS 1 760.00 2 sa STRIP 1 450.00 3 av ANOTHER VALUE HERE 1 2000.00
Muhc like printf(%-33s....etc). Any help or tips would be greatly appreciated

Replies are listed 'Best First'.
Re: Textview format, help please !
by moritz (Cardinal) on Jul 22, 2010 at 08:26 UTC
    Maybe use Text::Table to render the table?
    Perl 6 - links to (nearly) everything that is Perl 6.
      Hi, thanks for the reply. I gave Text::Table a go, but I still end up with output that looks like this:
      1 SA STRIP+ASSEMBLE 1 122.00 2 LOS LABOUR ON PARTS SUPPLY 1 1200.00 3 FBS FAN BLADE SUPPLY 1 1200.00
      I just used
      my $tb = Text::Table->new(); $tb->add($linenum,$kode,$waarde,$entry_qty,$entry_price);
      Can you give me some examples how to use the 'align =>' and/or is_sep functions?
        Maybe you've had a long day, but I gave Text::Table a try and got good results. Try this:
        #!/usr/bin/perl use strict; use warnings; use Text::Table; my $tb = Text::Table->new( "ID", "DESCRIPTION", "QTY", "PRICE" ); $tb->load( [ "1 los", "LABOR ON PARTS", 1, '760.00' ], [ "2 sa", "STRIP", 1, '450.00' ], [ "3 av", "VALUE", 1, '2000.00' ], ); print $tb;
        Maybe your problem is that you want to display ASCII art with a proportional font?

        Try to use a monospace font instead, then all the columns should align neatly.

Re: Textview format, help please !
by Utilitarian (Vicar) on Jul 22, 2010 at 10:31 UTC
    If you are liked printf, you'll love sprintf, returns a string formatted as specified

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Textview format, help please !
by biohisham (Priest) on Jul 22, 2010 at 16:57 UTC
Re: Textview format, help please !
by Generoso (Prior) on Jul 22, 2010 at 17:21 UTC

    You may what to try something like this.

    #!/usr/bin/perl use strict; my ($ID,$DESCRIPTION,$QTY,$PRICE); printf ("%s\t%-26.26s\t%s\t%s\n","ID", "DESCRIPTION ", " QTY", " + PRICE"); foreach (<DATA>) { chomp; ($ID,$DESCRIPTION,$QTY,$PRICE) = split /,/; printf ("%s\t%-24.24s\t%5.0f\t%10.2f\n",$ID,$DESCRIPTION."\t" x (l +ength($DESCRIPTION)<6),$QTY,$PRICE); } print "1\t2\t3\t4\t5\t6\t\n"; print '-' x (60),"\n"; __DATA__ 1 los,LABOR ON PARTS,1,760.00 2 sa,STRIP,1,450.00 3 av,VALUE,1,2000.00
Re: Textview format, help please !
by peokai (Novice) on Jul 26, 2010 at 08:23 UTC
    Thanks for the reply guys, I actually did it a totally different way, I used a treeview with a simplelist to add columns and just push the values into it, works like a charm as it auto adjusts the length of the values to fit nicely. Thanks for your replies :)