views:

81

answers:

3

I want to output strings into 8 columns, but I want to keep the spacing the same. I dont want to do it in HTML, but I am not sure how to do it normally. Example:

Something    Something     Something     Something     Something 
Else         Else          Else          Else          Else
Another      Another       Another       Another       Another

The amount of rows will change daily but the column number will always stay the same. What is the best way to do this?

+4  A: 

printf

    printf "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-11s\n",
            $column1, $column2, ..., $column8;

Change "11" in the template to whatever value you need.

mobrule
That is perfect thanks, when I googled it I found all these complicated methods.
shinjuo
As eluded, when things seem to get complicated, it generally means you have more control/options. `printf` is very powerful, but consider `format` if you are going to be printing formatted reports. `format` gives the ability to add headers/footers, which makes outputting to files easier to modify.
vol7ron
I dont need to get technical with headers or anything. It is just to have things output in a nice view on a sheet
shinjuo
Then `printf` is most likely what is needed. If your data is in an array, see my answer on how to pass the array values as parameters - just pass the whole array list, rather than specifying each node independently.
vol7ron
+4  A: 

You could use Perl's format. This is probably the "complicated" method that you don't understand, most likely because it gives you many options (left|center|right justification/padding, leading 0's, etc).

Perldoc Example:

Example:
   format STDOUT =
   @<<<<<<   @||||||   @>>>>>>
   "left",   "middle", "right"
   .
Output:
   left      middle    right

Here's another tutorial.


Working Example:

#!/usr/bin/perl -w    

use strict; 

   sub main{    
      my @arr = (['something1','something2','something3','something4','something5','something6','something7','something8']
                ,['else1'     ,'else2'     ,'else3'     ,'else4'     ,'else5'     ,'else6'     ,'else7'     ,'else8'     ]
                ,['another1'  ,'another2'  ,'another3'  ,'another4'  ,'another5'  ,'another6'  ,'another7'  ,'another8'  ]
                );

      for my $row (@arr) {
         format STDOUT =
@<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  
         @$row
.
         write;
      }

   }    

   main();   
vol7ron
+2  A: 

I would look at formatting, but I would do it using perl6's Form.pm, which you can obtain as Perl6::Form for perl5.

The reason for this is that the format builtin has a number of drawbacks, such as having the format statically defined at compile time (i.e building it dynamically can be painful and usually requires string eval), along with a whole list of other shortcomings, such as lack of useful field types (and you can't extend these in perl5).

lochii
`The amount of rows will change daily but the column number will always stay the same.` No need to build dynamically or use eval.
vol7ron