Monday 21 March 2016

Perl Repetition Operator "x"

Cited from How can I repeat a string N times in Perl?

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.

say ’-’ x 80;   # print row of dashes
my @ones = (1) x 80; # a list of 80 1’s
@ones = (5) x @ones;        # set all elements to 5



No comments:

Post a Comment