Broad Network


Wordwrap and Some Useful Perl String Features

Handling Strings in Perl Part 6

Perl Course

Foreword: In this part of the series, I talk about wordwrap, chop and chomp.

By: Chrysanthus Date Published: 23 Oct 2015

Introduction

This is part 6 of my series, Handling Strings in Perl. In this part of the series, I talk about wordwrap, chop and chomp. You should have read the previous parts of the series before reaching here, as this is a continuation.

Text Wrap
Assume that you have an array of lines of text, and no line has the newline (\n) character. You can break each line into more lines using \n. A line is broken at a word boundary. A single string is returned with more lines. You have to set the number of characters at or just before which the line is broken. The default line break separator is \n, but you can also set it.

To achieve all that, you use the Text::Wrap module that is installed with Perl. Try the following code, which has just one line in the array:

use strict;

use Text::Wrap;
    $Text::Wrap::columns = 20;
    $Text::Wrap::separator = "\n";

    my $str = "The quick brown foxes jumped over the lazy dogs.";
    my @arr = ($str);
    my $ret = wrap('', '', @arr);

    print $ret;

The output is:

    The quick brown
    foxes jumped over
    the lazy dogs.

You begin the code segment by bringing in the Text::Wrap module. You then set the maximum number of characters (columns) per line. Next, you can set the line break separator in double quotes. You continue with the strings and array. Then you call the wrap function, brought in with the module. As you can see from the output, the text has been wrapped.

The chop Function
The chop function is used to remove the last character of a string or the last character of each string in an array of strings. It can also be used to remove the last character of each value in a hash, but not the keys. The syntaxes are:

    chop VARIABLE

    chop( LIST )
    chop

If the argument is a string variable or string literal, the last character is removed and returned. In the case of a list, the value of the last element character is returned. At the end, the original string or array or hash is modified. Before we continue, remember, parentheses around function arguments are optional in Perl. Try the following code:

use strict;

    my $str = "Well, I am there";
    my $ret = chop($str);
    print $ret, "\n";
    print $str, "\n";

For the string of interest, e is removed and returned. The string, $str becomes short of e.

If there is no argument, the string of the special variable, $_is chopped. The no-argument action corresponds to the last syntax above.

If the argument is an array of strings, the last character of each string is removed. However, here, it is the last character of the value of the last element that is returned. Read and try the following code:

use strict;

    my @arr = ("one", "two", "three", "four");
    my $ret = chop (@arr);
    print $ret, "\n";
    print @arr, "\n";

The chop operation here, corresponds to the second syntax above.

The chomp Function
This function is used to remove the \n escape sequence at the end of a line (record) from a text file saved in disk. Try the following code:

use strict;

    my $str = "This is the man.\n";
    my $ret = chomp($str);
    print $ret, "\n";
    print $str, "\n";

The syntaxes for the chomp function are:

    chomp VARIABLE

    chomp( LIST )
    chomp

The above code corresponds to the first syntax where the argument is a variable. In the absence of the variable, the $_ special variable is used; and that corresponds to the third syntax.

You can have an array of text lines from a file where each line ends in \n. The second syntax above will remove the \n from the end of all the string elements (lines) of the array. Read and try the following code:

use strict;

    my @arr = ("This is the man.\n", "This is the woman.\n", "This is the child.\n", "This is the boy.\n");
    my $ret = chomp(@arr);
    print $ret, "\n";
    print @arr, "\n";

The return value is the number of chomping (chopping). This chomp operation corresponds to the second syntax above.

That is it for this part of the series.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK

Comments

Become the Writer's Fan
Send the Writer a Message