Re: [問題] 關於長字串文字排版

看板Perl作者 (小a)時間15年前 (2010/03/31 01:57), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串4/5 (看更多)
※ 引述《ForeverT (全家好神)》之銘言: : 感謝 : 好用的一招 學起來 : 但字數是未知的 : 格式是固定的 : 長字串可能會被削去 : 短字串可能空格太多 : 又該如何解呢? : 希望能依據最長字串做向右或向左對齊 : 謝謝 chomp(@TMP=<DATA>); foreach(@TMP) { foreach( map { length } split(/ /) ) { $i++; $LEN{$i}=$_ if($LEN{$i} < $_); } undef $i; } foreach(@TMP) { foreach( split(/ /) ) { $i++ and print ' '; # 分隔 # printf("%-$LEN{$i}s", $_); # 靠左對齊 # printf("%$LEN{$i}s", $_); # 靠右對齊 # 置中對齊 $LEN{0}=int(($LEN{$i}-length)/2); printf("%$LEN{0}s",undef); $LEN{0}=$LEN{$i}-$LEN{0}; printf("%-$LEN{0}s", $_); # 置中對齊 } undef $i; print "\n"; } __DATA__ xxxxxxxxxx xxxxxxxxx xxxxxxxxxxxxxxxx xxxxxxxxxxx xxxxx xxxxxxxxx 寫得不夠簡潔, 請見諒... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.192.148.69

03/31 14:18, , 1F
謝謝 研究研究
03/31 14:18, 1F
文章代碼(AID): #1BiZjwi8 (Perl)
文章代碼(AID): #1BiZjwi8 (Perl)