in reply to Re: Formatting of printed SQL Query output
in thread Formatting of printed SQL Query output

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

Replies are listed 'Best First'.
Re^3: Formatting of printed SQL Query output
by GrandFather (Saint) on Jun 29, 2007 at 07:08 UTC

    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

      ...or, since you ask for with borders (presumably to make it look like directly using your db's output capabilities), print your headers and initial set of borders outside the loop, and modify GrandFather's printf slightly (to provide the column-separating border, as follows:

      use strict; use warnings; print " Co.ID \t|\tNAME\n"; print "-------\t|------------------------\n"; 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\t|\t%s\n", split ' ', $_, 2 for @rows;

      OUTPUT:

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

      Minor Note: Since laziness is a virtue, check on the string repetition operator (for example, p25 of "Learning Perl," O'Reilly, Randal L. Schwartz & Tom Phoenix, which should be on your bookshelf) for a way to get the multiple "-"s with fewer keystrokes.
      hello all, thank you for your replies, they have been very helpful i used this command (as displayed above)
      printf "%6d %s\n", split ' ', $_, 2 for @rows;
      and I get this output displayed on my DOS screen
      16444 3M Singapore 0 10167 ABB Schweiz (CHHOS) - SAP 0 13916 ABB Turbo Systems AG - LVS 0 10163 ABB Turbo Systems AG - SAP 0 8683 ABC
      How do I get rid of the '0' that keeps appearing in between the lines??? thanks again

        You have blank lines or some such garbage in @rows.

        I strongly recommend that you use strictures (use strict; use warnings;). If you had warnings turned on you would have seen warnings like:

        Use of uninitialized value in printf at noname.pl line 16.

        interspersed through your output. Do you need to chomp your lines before they are added to the array?


        DWIM is Perl's answer to Gödel
Re^3: Formatting of printed SQL Query output
by runrig (Abbot) on Jun 29, 2007 at 07:21 UTC
    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 :-)