本帖最后由 523066680 于 2017-10-19 18:28 编辑
(Perl区没有话题也要水一个)
可以这样写:- use List::Util qw/max/;
- my $newstr = "T";
- my @lines = <DATA>;
- my $maxlen = max map { $_=~s/\r?\n$//;length($_) } @lines;
- grep { printf "%-${maxlen}s %s\n", $_, $newstr } @lines;
-
- __DATA__
- B B B B
- B B B
- B B
- B
复制代码 如果要尽量接近口语描述的话:- use List::Util qw/max/;
-
- sub get_each_line { (<DATA>) }
- sub get_each_len_of { map { $_=~s/\r?\n$//; length($_) } @_ }
- sub print_each_of { grep { printf "%-${_[2]}s %s\n", $_, $_[4] } @{$_[0]}; }
-
- $newstr = "T";
- my @lines = get_each_line;
- my $max = max get_each_len_of @lines;
- print_each_of \@lines, set_length => $max, append => $newstr;
-
- __DATA__
- B B B B
- B B B
- B B
- B
复制代码
- B B B B T
- B B B T
- B B T
- B T
复制代码
|