我有这样的档案,
SR Name Rollno Class
1 Sanjay 01 B
2 Rahul_Kumar_Khanna 09 A现在我需要在每个人之间加上“x”。所以看起来应该是
SR | Name |Rollno | Class|
1 | Sanjay |01 | B |
2 | Rahul_Kumar_Khanna|09 | A |我正在使用Perl6 6::form
my $text;
foreach my $line (@arr) {
my ($SR, $Name, $Rollno, $Class) = split (" ", $line);
my $len = length $Name;
$text = form
'| {||||||||} | {||||||||} | {||||||||} | {||||||||}|',
$SR, $Name, $Rollno, $Class;
print $text;
}到目前为止,我已经做过了,但是名字还没有正确开始。我在名字里加了额外的“”。是否有任何方法可以通过计算长度(如下面)来添加“AC.26”。我试过了,但出错了。
'| {||||||||} | {||||||||}x$len | {||||||||} | {||||||||}|',发布于 2020-09-22 13:06:36
问题#1
'| {||||||||} | {||||||||}x$len | {||||||||} | {||||||||}|'产生
| {||||||||} | {||||||||}x20 | {||||||||} | {||||||||}|但你想让
| {||||||||} | {||||||||||||||||||||} | {||||||||} | {||||||||}|为了这个,你会想
'| {||||||||} | {'.( "|" x $len ).'} | {||||||||} | {||||||||}|'问题#2
$len是当前行的name字段的长度。每一排都不一样。这是错误的,因为您希望每一行的输出都是相同的宽度。$len需要是最长名称字段的长度。
在启动循环之前,您需要为$len找到正确的值。
# Read in the data as an array of rows.
# Each row is an array of values.
my @rows = map { [ split ] } <>;
# Find the maximum width of each column.
my @col_lens = (0) x @{rows[0]};
for my $row (@rows) {
# Skip the blank line after the header.
next if !@$row;
for my $col_idx (0..$#$row) {
my $col_len = $row->[$col_idx];
if ($col_lens->[$col_idx] < $col_len) {
$col_lens->[$col_idx] = $col_len;
}
}
}
my $form =
join "",
"| ",
"{".( "|"x($col_lens[0]-2) )."}",
" | ",
"{".( "|"x($col_lens[1]-2) )."}",
" | ",
"{".( "|"x($col_lens[2]-2) )."}",
" | ",
"{".( "|"x($col_lens[3]-2) )."}",
" |";
for my $row (@rows) {
if (@$row) {
print form($form, @$row);
} else {
print "\n";
}
}https://stackoverflow.com/questions/64003805
复制相似问题