Home arrow Perl Programming arrow Page 2 - Perl: Concatenating Text and More

Making Copies - Perl

This marks the finale of our coverage of text in Perl (until we get into some more of the advanced Perl features at any rate). We left off with the here document and how to use it to display text exactly as we type it in, using a Mark Twain poem that he had written for his daughter's tombstone (which interestingly enough was a rewrite of another poet's poem). We also learned a little bit about ASCII and the values as they pertain to text, showcasing the 93 visible characters.

TABLE OF CONTENTS:
  1. Perl: Concatenating Text and More
  2. Making Copies
  3. Chomping It Up Pac-Man Style
  4. Transformers...More than Meets the Eye
By: James Payne
Rating: starstarstarstarstar / 5
March 10, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More Perl Programming Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: