Repeat strings with the repetition operator Repeat strings with the repetition operator

You get the idea - Perl has a repetition operator (x) that repeats the scalar or list on its left by the number on it’s right (like multiplication).

my $string = 'I like chocolate';
print $string x 10;
#results

I like chocolateI like chocolateI like chocolateI like chocolateI like chocolateI like chocolateI like chocolateI like chocolateI like chocolateI like chocolate

You may be able to think of more useful employment for this operator, like this:

perl -CS -e 'print qq!\N{U+2661} \N{U+2665}! x 1000;'

Tags

David Farrell

David is the founder and editor of PerlTricks.com. An organizer of the New York Perl Meetup, he works for ZipRecruiter as a software developer.

Browse their articles