Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create columnar output in Perl?

Tags:

printf

perl

!/usr/bin/env perl

 use warnings;
 use strict;

 my $text = 'hello ' x 30;

 printf "%-20s : %s\n", 'very important text', $text;

The output of this script looks more ore less like this:

very important text      : hello hello hello  hello
hello hello hello hello hello hello hello hello
hello hello hello hello hello hello hello hello
...

But I would like an output like this:

very important text: hello hello hello hello
                     hello hello hello hello
                     hello hello hello hello
                     ...

I forgot to mention: The text should have an open end in the sense that the right end of the textlines should align corresponding to the size of the terminal.

How could I change my script to reach my goal?

like image 297
sid_com Avatar asked Mar 20 '10 08:03

sid_com


2 Answers

You can use Text::Wrap:

use strict;
use Text::Wrap;

my $text = "hello " x 30;
my $init = ' ' x 20;
$Text::Wrap::columns = 80;

print wrap ( '', $init,  'very important text : ' . $text );
like image 139
justintime Avatar answered Sep 29 '22 06:09

justintime


Try this ,

use strict;
use warnings;

 my $text = 'hello ' x 30;

 $text=~s/((\b.+?\b){8})/$1\n                       /gs;
 printf "%-20s : %s\n", 'very important text', $text;
like image 40
karthi_ms Avatar answered Sep 29 '22 07:09

karthi_ms