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

Chomping It Up Pac-Man Style - 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 you want to chop off the end part of a string, you can do so using either the chop or chomp function. The chop function removes the last character of a string and returns what it chopped off, like so:


#!/usr/bin/perl

 

$scary="Look out it's Lorena Bobbitt";

$a=chop($scary);

print $scary;

print "\nOh God...I chopped off a $a! Now she's gonna be pissed!";

This will print out:

  Look out it's Lorena Bobbit

  Oh God...I chopped off a t! Now she's gonna be pissed!

In the above example we assigned a value to the variable $scary, then used the chop function to chop off the last character and assign that removed character to the variable $a. We then printed $scary and a sentence with the chopped off character in it.

Chomp works in a similar fashion, and is said to be safer than using chop, as chomp removes two line-ending characters. Consider this code, which is the same as the above code, only I have added a newline escape character to it, to insert a space between text:


#!/usr/bin/perl

 

$scary="Look out it's Lorena Bobbitt\n";

$a=chop($scary);

print $scary;

print "\nOh God...I chopped off a $a! Now she's gonna be pissed!";

This prints out:

  Look out it's Lorena Bobbitt

  Oh God...I chopped off a

  ! Now she's gonna be pissed!

As you can see, in this instance chop took our newline character instead of the t as we had intended. And instead of returning a t as I had wanted, it returned a newline, messing up the flow of my text. This is why it is safer to use chomp. Here it is in action:


#!/usr/bin/perl

 

$scary="Look out it's Lorraina Bobbittn";

$a=chomp($scary);

print $scary;

print "\nOh God...I chopped off a $a! Now she's gonna be pissed!";

Tada! Note that if you have Perl version 4, chomp doesn't work.



 
 
>>> 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: