如何在 Perl 中重复一个字符串 N 次?

发布于 2024-07-08 19:39:36 字数 300 浏览 7 评论 0原文

在 Python 中,如果我这样做:

print "4" * 4

我得到

> "4444"

In Perl,我会得到

> 16

Is there a simple way to do the previous in Perl?

In Python, if I do this:

print "4" * 4

I get

> "4444"

In Perl, I'd get

> 16

Is there an easy way to do the former in Perl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ぽ尐不点ル 2024-07-15 19:39:36
$ perl -e 'print "4" x 4; print "\n"'
4444

x 运算符记录在 perldoc perlop 中。 顺便说一句,这里的二进制意味着一个带有两个参数的运算符,而不是由位组成。

二进制“x”是重复运算符。 在标量环境中或者如果
左操作数不包含在括号中,它返回一个字符串,其中包含
左操作数重复右操作数指定的次数
操作数。 在列表上下文中,如果左操作数括在括号中
或者是由“qw/STRING/”组成的列表,它重复该列表。 如果对的话
操作数为零或负数,则返回空字符串或空
列表,具体取决于上下文。

       print '-' x 80;             # Print row of dashes

       print "\t" x ($tab/8), ' ' x ($tab%8);      # Tab over

       @ones = (1) x 80;           # A list of 80 1’s
       @ones = (5) x @ones;        # Set all elements to 5

perl -e 旨在从命令行执行 Perl 代码:

$ perl --help
Usage: perl [switches] [--] [programfile] [arguments]
  
  -e program     one line of program (several -e's allowed, omit programfile)
$ perl -e 'print "4" x 4; print "\n"'
4444

The x operator is documented in perldoc perlop. Here binary means an operator taking two arguments, not composed of bits, by the way.

Binary "x" is the repetition operator. In scalar context or if the
left operand is not enclosed in parentheses, it returns a string consisting
of the left operand repeated the number of times specified by the right
operand. In list context, if the left operand is enclosed in parentheses
or is a list formed by "qw/STRING/", it repeats the list. If the right
operand is zero or negative, it returns an empty string or an empty
list, depending on the context.

       print '-' x 80;             # Print row of dashes

       print "\t" x ($tab/8), ' ' x ($tab%8);      # Tab over

       @ones = (1) x 80;           # A list of 80 1’s
       @ones = (5) x @ones;        # Set all elements to 5

perl -e is meant to execute Perl code from the command line:

$ perl --help
Usage: perl [switches] [--] [programfile] [arguments]
  
  -e program     one line of program (several -e's allowed, omit programfile)
聽兲甴掵 2024-07-15 19:39:36

在 Perl 中,您需要使用“x”运算符。

之间的区别

"4" x 4

注意和

("4") x 4

前者产生重复的字符串:

"4444"

后者产生重复的列表:

("4", "4", "4", "4")

In Perl, you want to use the "x" operator.

Note the difference between

"4" x 4

and

("4") x 4

The former produces a repeated string:

"4444"

the latter a repeated list:

("4", "4", "4", "4")
木落 2024-07-15 19:39:36

Perl 中的情况非常相似

print "4" x 4;

It's very similar in Perl

print "4" x 4;
花伊自在美 2024-07-15 19:39:36

FWIW,它也是 Perl 中的print 4 x 4

一般来说,在 Perl 中,运算符是单态的,即。 对于字符串语义、数字语义、按位语义等,您有不同的运算符集,只要有意义,操作数的类型在很大程度上并不重要。 当您将数字运算符应用于字符串时,字符串首先会转换为数字,然后您将获得所需的运算(例如乘法),而当您将字符串运算符应用于数字时,它会转换为字符串,然后您将得到所需的运算。获取您要求的操作(例如重复)。 Perl 首先关注操作符,然后才是操作数的类型——如果它确实在意的话。

这与 Python 和大多数其他语言相反,在 Python 和大多数其他语言中,您使用一组运算符,并且操作数的类型决定您实际获得的语义 - 即。 运算符是多态的。

FWIW, it’s also print 4 x 4 in Perl.

In general, in Perl, operators are monomorphic, ie. you have different sets of operators for string semantics, for numeric semantics, for bitwise semantics, etc., where it makes sense, and the type of the operands largely doesn’t matter. When you apply a numeric operator to a string, the string is converted to a number first and you get the operation you asked for (eg. multiplication), and when you apply a string operator to a number, it’s turned into a string and you get the operation you asked for (eg. repetition). Perl pays attention to the operator first and the types of the operands only second – if indeed it pays them any mind at all.

This is the opposite of Python and most other languages, where you use one set of operators, and the types of the operands determine which semantics you’ll actually get – ie. operators are polymorphic.

誰認得朕 2024-07-15 19:39:36

如果你想打印 10 个字符“A”,你也可以

perl -e 'print "A" x 10'; echo

用输出来执行此示例

user@linux:~$ perl -e 'print "A" x 10'; echo
AAAAAAAAAA
user@linux:~$ 

If you want to print 10 character "A"s, you can also do this

perl -e 'print "A" x 10'; echo

Example with output

user@linux:~$ perl -e 'print "A" x 10'; echo
AAAAAAAAAA
user@linux:~$ 
阳光的暖冬 2024-07-15 19:39:36

到目前为止给出的所有答案都没有提到运算符x不仅适用于字符串文字,而且还适用于字符串或表达式评估为像

use feature 'say';

my $msg = "hello ";
say $msg x 2;
say chr(33) x 3;

这样的

hello hello
!!!

字符串,更重要x将表达式自动转换为字符串(如果还没有)(感谢 ggorlen 为我指明了这个方向!)。 因此,例如

say 4 x 2;
say [$msg] x 2;

将导致类似以下内容的输出

44
ARRAY(0x30ca10)ARRAY(0x30ca10)

All answers, given so far, missed mentioning that the operator x does not only work on string literals, but also on variables that are strings or expressions that evaluate to strings like

use feature 'say';

my $msg = "hello ";
say $msg x 2;
say chr(33) x 3;

like this

hello hello
!!!

and, even more important, x does an automatic conversion of expressions into strings if they aren't already (thanks to ggorlen for pointing me into that direction!). So for example

say 4 x 2;
say [$msg] x 2;

will result in something like the following as output

44
ARRAY(0x30ca10)ARRAY(0x30ca10)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文