我有一个字符串
my $str = 'one ; two - three; four - five | six - seven; eight ; nine-ten | eleven - twelve | thirteen - fourteen; ';
我需要输出如下所示
one ; two - three; four - five ; eight ; nine-ten ;
; ; six - seven ; ; eleven - twelve ;
; ; ; ; thirteen - fourteen ;
按';'分割字段,并按' |'分割。 ))) 请帮帮我
答案 0 :(得分:3)
这可以实现您的目标:
#!/usr/bin/perl
use warnings;
use strict;
use List::Util qw(max);
my $str = 'one ; two - three; four - five | six - seven; eight ; nine-ten | eleven - twelve | thirteen - fourteen; ';
my @fields = map { [ split /\s*\|\s*/ ] } split /\s*;\s*/, $str;
my $max_row = max map { scalar(@$_) } @fields;
foreach my $row (0 .. $max_row-1) {
foreach my $col (@fields) {
my $width = max map { length ($_) } @$col;
printf " %-*s ;", $width, $col->[$row] ? $col->[$row] : " ";
}
print "\n";
}