Reputation: 440
I'm writing in Perl to direct my report in table format but I'm having issue whenever the data filling is texted too long. Is there a way a write the code so that it can wrapped the lines to m ultiple. Here is my code and output. Thanks!
printf "\n%-${max1}s %-${max2}s %-15s\n", "====", "====", "=====" ;
printf "%-${max1}s %-${max2}s %-15s\n", "Code", "Item", "Group" ;
printf "%-${max1}s %-${max2}s %-15s\n", "====", "====", "=====" ;
foreach my $f (@{$rpt_ptr}) {
printf "%-${max1}s %-${max2}s %-15s\n", "$$f{code}", "$$f{item}", "$$f{group}",
}
The output table will be extended to too long if I have the Item list which is too long, ie:-
==== ===== =====
Code Item Group
==== ===== =====
A1011 aaaaaa, bbbbb, ccccc, ddddd, eeeee, fffff, ggggg, hhhhh, iiiii, jjjjjj, kkkkk, llll B
B101 cccccc A
I'm aspected if I can comes out with a table such like:
==== ===== =====
Code Item Group
==== ===== =====
A1011 aaaaaa, bbbbb, ccccc, ddddd, B
eeeee, fffff, ggggg, hhhhh,
iiiii, jjjjjj, kkkkk, llll
B101 cccccc A
How am I going to achieve that?
Upvotes: 1
Views: 2261
Reputation: 440
I finally get it done by following code, posted here to share:-
foreach my $f (@{$rpt_ptr}) {
# printf "%-${max1}s %-${max2}s %-15s\n", "$$f{code}", "$$f{item}", "$$f{group}",
format STDOUT_TOP =
=============== ========================= =========
Code Item Group
=============== ========================= =========
.
format STDOUT =
@<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<
$$f{code} $$f{item} $$f{group}
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<
$$f{item}
.
write ;
}
To all who have responded to my question, big thanks for your helps, I greatly appreciate it!
Upvotes: 0
Reputation: 107090
Perl was originally written to format text files. It includes a rather interesting form generating facility, including the ability to wrap lines the way you've specified. I have not seen it used in a long, long while, but it's still part of the language.
It's in the Perldoc under perlform.
#! /usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
my ($invoice, $description, $amount);
while ( chomp ( my $line = <DATA> ) ) {
( $invoice, $description, $amount ) = split /:/ => $line;
write;
}
format STDOUT_TOP =
Invoice Description Amount
======= =========== =======
.
format STDOUT =
@<<<<<< ^<<<<<<<<<<<<<< @###.##
$invoice, $description, $amount
^<<<<<<<<<<<<<<
$description
^<<<<<<<<<<<<<<
$description
^<<<<<<<<<<<<<<
$description
^<<<<<<<<<<<<<<
$description
.
__DATA__
One:This line contans a lot of text that I have to process and I have no idea what it is:2.30
Two:Here's another line that contains lots of data that won't fit on a single line:4.40
Three:And one more line of extra long data that may or may not fit on a single line:5.10
Invoice Description Amount
======= =========== =======
One This line 2.30
contans a lot
of text that I
have to process
and I have no
Two Here's another 4.40
line that
contains lots
of data that
won't fit on a
Three And one more 5.10
line of extra
long data that
may or may not
fit on a single
Upvotes: 3
Reputation: 42179
If you want something very basic/specific (not robust), force a line break every fourth comma:
In your loop before the printf
:
$$f{item} =~ s/(,[^,]+,[^,]+,[^,]+,)/$1\n/g;
Best option is probably to use perlform (5.8.8 for you) as David suggested
Upvotes: 0
Reputation: 6798
format
s are really the way to go.
Here's a solution using Perl6::Form:
use Perl6::Form;
my @data = (
['A1011', 'aaaaaa, bbbbb, ccccc, ddddd, eeeee, fffff, ggggg, hhhhh, iiiii, jjjjjj, kkkkk, llll', 'B'],
['B101', 'cccccc', ''],
);
print form
"==== ===== =====",
"Code Item Group",
"==== ===== =====";
foreach (@data) {
print form
"{<<<<}{[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[} {<<}", @$_ ;
}
Upvotes: 1
Reputation: 1342
You can use regexp for this wrapping the text.
Since your are printing in the file line by line,
you can take every line in a scalar variable and check the length of that scalar variable and insert new line character \n
.
like:
$var = "\n%-${max1}s %-${max2}s %-15s\n", "====", "====", "=====" ;
$var .= "%-${max1}s %-${max2}s %-15s\n", "Code", "Item", "Group" ;
$var .= "%-${max1}s %-${max2}s %-15s\n", "====", "====", "=====" ;
## Adding new line character after 20 characters.
my $startIndex = 0;
while( ($startIndex < length($var) && (length($var) > 20) ) {
print substr($var, $startIndex, 20) . "\n";
$startIndex = $startIndex + 20;
}
Upvotes: 0