Perl Programming Page 2 - Perl: Concatenating Text and More |
If we want to repeat a string a bunch of times, we can do so by multiplying it. Let's say I walk into a wrestling arena and am fighting the Badass Bulgarian. Here is a program that demonstrates what the crowd might say, if they know what is good for them: #!/usr/bin/perl
$chant="USA! "; $chantTwo="Bulgaria blows! "; $chantThree="James Payne! "; print $chant x 10; print "\n" . $chantTwo x 10; print "\n" . $chantThree x 10; This results in: USA! USA! USA! USA! USA! USA! USA! USA! USA! USA! Bulgaria blows! Bulgaria blows! Bulgaria blows! Bulgaria blows! Bulgaria blows! Bulgaria blows! Bulgaria blows! Bulgaria blows! Bulgaria blows! Bulgaria blows! James Payne! James Payne! James Payne! James Payne! James Payne! James Payne! James Payne! James Payne! James Payne! James Payne! But what if we wanted to not have the words listed on the same line? #!/usr/bin/perl
$chant="\tUSA! \n"; $chantTwo="\tBulgaria blows! "; $chantThree="James Payne! "; print $chant x 10; This prints the variable $chant 10 times, skipping a line and adding a tab at the beginning of each new line, resulting in the following: USA! USA! USA! USA! USA! USA! USA! USA! USA! USA!
blog comments powered by Disqus |
|
|
|
|
|
|
|