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;'


This article was originally posted on PerlTricks.com.

Tags

David Farrell

David is the editor of Perl.com. An organizer of the New York Perl Meetup, he works for ZipRecruiter as a software developer, and sometimes tweets about Perl and Open Source.

Browse their articles

Feedback

Something wrong with this article? Help us out by opening an issue or pull request on GitHub