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

Hello all! I have a small question. I am writing this program in perl, and for one of the queries i am printing several values of two field names. this is printed on the cmd prompt screen but its not formatted properly. How can I create columns to align all the printed information? Thanks

Replies are listed 'Best First'.
Re: Formatting of printed SQL Query output
by jbert (Priest) on Jun 29, 2007 at 06:51 UTC
    There are lots of ways of doing this in perl. I'd probably use sprintf to format a string and then print it out, but you could also just try using tab characters (put a literal \t in your output string).

    You could even use the old and much-maligned perl formats perlform, but lots of people feel that they have outlived their usefulness and shouldn't be used any more.

      You can split your out putput based on the delimiter and I hope its spaces in your case (shown as \s+ below). And print these one by one giving a tab (shown as \t) between the columns. This should provide you with some basic alignment.
      foreach my $line(@query_output) { my ($comp_id,$comp_info)=split(/\s+/,<$line>); print "$comp_id\t$comp_info\n"; }
      Hope it would of some help.
      cheers, Syntex would be this: my ($comp_id,$comp_info)=split(/\s+/,$line);
Re: Formatting of printed SQL Query output
by GrandFather (Saint) on Jun 29, 2007 at 05:24 UTC

    How about you show us a sample of what you've got, what it prints, and what you'd like to see? We don't want to see your DB code, just the print code printing a couple of representative lines provided in the same structure you'd have gotten from the DB handling code. Sample should run stand alone and should only need to be a dozen or so lines long.


    DWIM is Perl's answer to Gödel
      hi thanks for your reply.. my output looks something like this, an ID followed by the corresponding company name. I just want to create two clear columns, with borders that one column name is named 'Company ID' and have all the list of IDs, and another column called 'Company Name'.
      16444 3M Singapore 10167 ABB Schweiz (CHHOS) - SAP 13916 ABB Turbo Systems AG - LVS 10163 ABB Turbo Systems AG - SAP 8683 ABC 20006 ABX Logistics International GmbH 20007 ABX Logistics Rheinkraft GmbH 23824 ABX TRAX 23259 AC CONNECT|
      .......

        I doubt very much that your data arrives as strings, but you've not given us anything else so:

        use strict; use warnings; my @rows = ( '16444 3M Singapore', '10167 ABB Schweiz (CHHOS) - SAP', '13916 ABB Turbo Systems AG - LVS', '10163 ABB Turbo Systems AG - SAP', '8683 ABC', ); printf "%6d %s\n", split ' ', $_, 2 for @rows;

        Prints:

        16444 3M Singapore 10167 ABB Schweiz (CHHOS) - SAP 13916 ABB Turbo Systems AG - LVS 10163 ABB Turbo Systems AG - SAP 8683 ABC

        See printf.


        DWIM is Perl's answer to Gödel
        If you want borders and headers and such, maybe look at some CPAN modules. I took a quick look and found Text::Table and Text::ASCIITable...there are probably others. Or maybe your needs are simple enough that perl's built in formats will do.

        Update: I vaguely recall some module that was more directly DBI related...maybe some DBIx module?...anyway, I leave finding one if such exists up to the reader :-)