Perl 演算子 x を使った文字列の繰り返し

演算子xを使った文字列の繰り返し


勉強していて楽しかったので記録。文字列を文字で作った飾り枠で囲ったりしました。 演算子xの使って'-'を40個表示する。

print '-' x 40, "\n";

> ----------------------------------------

次に配列@textの各要素を文字で作った飾り枠で囲みます。

my @text = (
     "There's more than one way to do it.",
     "Write once, run anywhere.",
     "Programming Lesson",
);
foreach my $str (@text) {
  print '+','-' x length($str),'+',"\n";
  print '|',$str,'|',"\n";
  print '+','-' x length($str),'+',"\n";
}

> +-----------------------------------+
> |There's more than one way to do it.|
> +-----------------------------------+
> +-------------------------+
> |Write once, run anywhere.|
> +-------------------------+
> +-------------------+
> |Programming Lesson.|
> +-------------------+

'-'をいくつ繰り返すかはlength($str)で得ています。

参考文書

新版Perl言語プログラミングレッスン入門編

新版Perl言語プログラミングレッスン入門編