"4444" In Perl, I would get > 16 Is the...">

How can I repeat a string N times in Perl?

In Python, if I do this:

print "4" * 4 

I get

 > "4444" 

In Perl, I would get

 > 16 

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

+46
operators string perl repeat
Nov 10 '08 at
source share
6 answers
 $ perl -e 'print "4"x4; print "\n"' 4444 

The x statement is documented in perldoc perlop. Here, binary means that the operator takes two arguments, but does not consist of bits.

The binary "x" is the repeat operator. In a 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 indicated by the right operand. In the context of a list, if the left operand is enclosed in parentheses or it 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 empty 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 designed to execute Perl code from the command line:

 $ perl --help
 Usage: perl [switches] [-] [programfile] [arguments]
  
   -e program one line of program (several -e allowed, omit programfile)
+84
Nov 10 '08 at 10:02
source share

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

Pay attention to the difference between

 "4" x 4 

and

 ("4") x 4 

The first creates a repeating line:

 "4444" 

last duplicate list:

 ("4", "4", "4", "4") 
+38
Nov 10 '08 at 11:28
source share

It is very similar to Perl.

 print "4" x 4; 
+16
Nov 10 '08 at 10:05
source share

FWIW, its also print 4 x 4 in Perl.

In general, in Perl, operators are monomorphic, i.e. you have different sets of operators for string semantics, for numerical semantics, for bitwise semantics, etc., where this makes sense, and the type of operands doesn't really matter much. When you apply a numerical operator to a string, the string is first converted to a number, and you get the operation you requested (for example, multiplication), and when you apply the string operator to a number, it turns into a string, and you do the operation you asked (e.g. repetition). Perl pays attention to the operator first, and the types of operands - only to the second - if it really 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 operands determine what semantics you really get, i.e. operators are polymorphic.

+8
Nov 10 '08 at 13:17
source share

All the answers given so far are not mentioned that the x operator works not only on string literals, but also on string variables or expressions that build strings:

 $msg = "hello "; print $msg x 2; print ($msg x 2) x 2; 
+1
May 7 '15 at 12:19
source share

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

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

Exit example

 user@linux:~$ perl -e 'print "A" x 10'; echo AAAAAAAAAA user@linux:~$ 
+1
Jun 26 '17 at 9:21 on
source share



All Articles