string 如何在 Perl 中重复 N 次字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/277485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I repeat a string N times in Perl?
提问by izb
In Python, if I do this:
在 Python 中,如果我这样做:
print "4" * 4
I get
我得到
> "4444"
In Perl, I'd get
在 Perl 中,我会得到
> 16
Is there an easy way to do the former in Perl?
有没有一种简单的方法可以在 Perl 中完成前者?
回答by Vinko Vrsalovic
$ perl -e 'print "4"x4; 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.
x 运算符记录在 perldoc perlop 中。顺便说一下,这里的 binary 意味着一个带有两个参数的运算符,而不是由位组成。
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.
二进制“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
is meant to execute Perl code from the command line:
perl -e
旨在从命令行执行 Perl 代码:
$ perl --help Usage: perl [switches] [--] [programfile] [arguments] -e program one line of program (several -e's allowed, omit programfile)
回答by bart
In Perl, you want to use the "x" operator.
在 Perl 中,您想使用“x”运算符。
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")
回答by The Archetypal Paul
It's very similar in Perl
它在 Perl 中非常相似
print "4" x 4;
回答by Aristotle Pagaltzis
FWIW, it's also print 4 x 4
in Perl.
FWIW,它也在print 4 x 4
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.
通常,在 Perl 中,运算符是单态的,即。对于字符串语义、数字语义、按位语义等,您有不同的运算符集,这很有意义,并且操作数的类型在很大程度上无关紧要。当您将数字运算符应用于字符串时,字符串首先被转换为数字,然后您会得到您要求的运算(例如乘法),当您将字符串运算符应用于数字时,它会变成一个字符串,然后您获得您要求的操作(例如重复)。Perl 首先关注操作符,然后关注操作数的类型——如果它真的不介意的话。
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.
这与 Python 和大多数其他语言相反,在 Python 和大多数其他语言中,您使用一组运算符,并且操作数的类型决定了您实际获得的语义 - 即。运算符是多态的。
回答by Wolf
All answers, given so far, missed to mention that the operator x
does not only work on string literals but also on string variables or expressions that build strings:
到目前为止给出的所有答案x
都没有提到运算符不仅适用于字符串文字,还适用于字符串变量或构建字符串的表达式:
$msg = "hello ";
print $msg x 2;
print ($msg x 2) x 2;
回答by Charlotte Russell
If you want to print 10 character "A"s, you can also do this
如果要打印 10 个字符的“A”,也可以这样做
perl -e 'print "A" x 10'; echo
Example with output
输出示例
user@linux:~$ perl -e 'print "A" x 10'; echo
AAAAAAAAAA
user@linux:~$