Perl chomp Function

Perl chomp Menu:

1. The syntax forms
    1.1. chomp VARIABLE
    1.2. chomp (LIST)
    1.3. chomp
2. Exercises

1. The syntax forms

We’ll examine below Perl chomp function and how to use it in different contexts. The snippet code examples will point out various ways of using it and will clarify and highlight some interesting particular aspects. Please take your time and have a look at the following considerations, which will help you understand better its features.

There are three syntax forms for this function:

chomp (LIST)

1.1. chomp VARIABLE

If we use the first syntax form, we call this function having as argument the name of a variable. In this case the function will remove any trailing string at the end of the variable. Perl chomp function will not remove all whitespace characters by default, but only the ones that match the trailing string currently stored in the special variable $/ - also known as the input record separator. If there is not any trailing character at the end of the variable, the variable will remain unchanged.

Please note that there are significant differences between Perl chomp and Perl chop functions. While the chop function deletes the last ending variable character regardless of whatever it is, the Perl chomp function checks whether the last character matches the input line separator and only then it deletes it. Perl chomp function returns the number of characters removed.

This function is far more than mere a common function and you’ll use it especially when you are reading some text from the keyboard through the special file handle STDIN. The standard input stream will add a newline character (usually\n) at the end of each line, and you can get rid of it very simply by calling the Perl chomp function. No need to use any sophisticated regular expression for this.

For instance, look at the next code:

print "User name: ";
$name = ;
chomp $name;
print "Your user name is : $name\n";

This code will read from keyboard the user name and it will store it in the variable $name. This variable will have at the end a newline character, inserted after the user was hitting the return key. The Perl chomp function will take it off.

You can actually use the Perl chomp function with an lvalue, including an assignment. For instance, in the example above, we could replace the line two and three with the next line:

chomp ($name = );

Generally, Perl chomp() function removes only the last newline character but what if there are more? In this case you can play with the special variable $/, using the paragraph mode by setting $/ to "", as in the next example:

$/ = "";
$v = "\n\nsome text here\n\n\n\n";
$nr = chomp $v;

After executing this code, the first two newlines will remain unchanged, but the last fourth will be removed. The variable $nr will be set to 4 – the number of newline characters removed. Be careful, however, when you alter the content of the special variable $/, and restore it to its default value (\n), when you consider necessary.

1.2. chomp (LIST)

The second syntax form is for lists. You can chomp any array or hash (associative array), but we will be examining only one-dimensional ones here, without any reference. It doesn’t matter if you refer to arrays or hashes but don’t forget to put the array or hash inside the parenthesis of the Perl chomp function, otherwise the result could be one that you wouldn’t expect.

For beginning with, we will consider the case of arrays. To illustrate how to use Perl chomp function with an array, take a moment to examine the next snippet code:

$nr = chomp(@colors = );
print "\@colors = @colors\n";
print "Newlines removed: $nr";

This code will read all lines up to EOF and will chomp them off; the number of newlines removed is equal with the number of lines read.

Here is an example of the output:

@colors = red yellow white blue
Newlines removed: 4

Another way to do this is by using a while loop, if you want:

@colors = ();
while($line = )
  $nr+=chomp $line;
  push(@colors, $line);
print "\@colors = @colors\n";
print "Newlines removed: $nr";

The output will be identical as in the previous example shown above.

Next, let’s see an example when the Perl chomp function is called with a hash as argument. This will be illustrated in the following code segment. But before seeing the example, I want to specify that, like as in the case of the chop() function, only the values of a hash are chomped, the keys being left unchanged. After processing all the elements of a hash, the Perl chomp function will return the number of chomped characters.

# read the pair elements of the hash from STDIN 
chomp(%fruitsColors = );
# the next loop prints the keys/values of the hash
foreach $key (keys %fruitsColors)
  print "Key: $key, Value: $fruitsColors{$key}\n";

For this code, here is an output example:

dark blue
^Z in Windows (^d in Linux)
Key: plum
, Value: dark blue
Key: apricot
, Value: yellow
Key: cherry
, Value: red

As you see in the example above, the keys haven’t been chomped (by instance, the key "plum" ends with a newline character and because of this its corresponding value was printed on a new line in the cmd window).

Let’s look at the same example, but in this case we will initialize the hash by coding:

# create a new hash – fruitsColors 
# and assigning some elements to it
$fruitsColors{"plum\n"}="dark blue\n";
# chomp the hash
$nr = chomp(%fruitsColors);
# the next loop prints the keys/values of the hash
foreach $key (keys %fruitsColors)
  print "Key: $key, Value: $fruitsColors{$key}\n";
print "newlines removed: $nr\n";

The output for this short script:

Key: apricot
, Value: yellow
Key: plum
, Value: dark blue
Key: cherry
, Value: red
newlines removed: 3

As you see in this example, the number of newlines removed was 3 meanwhile the Perl hash keys remained "unchomped".

1.3. chomp

The last syntax form of Perl chomp function is that when the argument variable is omitted. Let’s work around through one of the examples shown below.

Quick example …

# define an array
@numbers = ("one\n", "two\n", "three\n", "four\n");
# let’s loop through its elements using foreach 
foreach (@numbers) {
print "@numbers\n";
# displays one two three four

The foreach statement loop variable is missing and that means that foreach will use for iteration the special variable $_. At each step of iteration:
  • the current element of the array will be stored in $_
  • the value stored in $_ will be chomped by its ending symbol

As a rule of thumb, we conclude that Perl chomp() function avoids uncertainty about whether a line of input has a newline character or not. If you use handle to input some information, you should always "chomp" the line of input immediately after reading it.

2. Exercises

Through these exercises you have the opportunity to try yourself to write some script code where you can use the Perl chomp function. These exercises are completely covered in my membership site mpihowto where I show you how to play with this important function in detail.
1. Read a word from STDIN into a scalar variable and define a scalar reference to this variable. Next, chomp the scalar reference and print the number of the removed characters.
2. Initialize an anonymous array with the following elements "John\n", "Peter", "Alice\n" and define a reference to this array. Next, chomp the array reference, print the array content and the number of the removed characters.
3. Initialize an anonymous hash with the following elements: "John\n", "23", "Peter", "45\n", "Alice\n", "32" and define a reference to this hash. Next, chomp the hash reference globally. What have you to say about the hash keys? Now chomp the keys of the hash one by one, using while and each. At the end, print the number of removed characters and print the hash using the Data::Dumper module. Give another script example where you copy the hash into another hash, chomping the keys and values one by one.
4. Give an example where you use chomp in a single line along with STDIN
5. Use the $/ special variable and the chomp function to remove all the trailing newlines of the following string: "some text here\n\n\n\n".
6. Use foreach to chomp all the elements of the folowing array: ("1\n", "2\n", "3\n", "4\n")
7. You have a text file with the following lines:
Write a short script where to read this file and finally print the cities from Europe.
8. Give a script example where you need to chomp large data.
9. Read the elements of an array from STDIN, using chomp and push in a while loop.
10.Give a script example where you read a csv file line by line, chomp each line and next split the content of the line into different variables.
11. Give a script example where you chomp the content of the following array of arrays:
  @AOA = (
         ["a11\n", "a12\n"],
         ["a21\n", "a22\n"],
12. Give a script example where you chomp the content of the following hash of hashes:
  %HOH = ( 
         John => { Age => "22\n", Sex => "M\n" },
         Paul => { Age => "25\n", Sex => "M\n" },
         Mary => { Age => "20\n", Sex => "F\n" },
13. You have the following pseudo-datafile:
  This \
  is a \
  multiline string
  This is \
  another one
Write a short code where to use chomp when you have to read multiline strings.
Your code must produce the folowing output:
  This is a multiline string
  This is another one
14. Give a script example about how you can use chomp with a binary file:
    - initialize an array with a few string elements
    - create from this array a binary file with the records delimitated by "\0" separator
    - empty the initial array
    - set the $/ special variable with "\0"
    - read the binary file line by line, chomp the "\0" rightmost character from the current line and append this line to our array
    - print the content of the array (you need to get back the initial content of the array)


Well, you can use the Perl chomp function in conjunction with the $/ (the input record separator) in many ways and in different contexts. The most frequently used are described in my Perl membership site mpihowto where I show you how to play with this important function in detail.

In this site I illustrate each of the following topics with a few snippet codes. If some of the examples are simple enough to help you understand the topic better, others are more complex and use different topics to achieve the specific tasks. From string and array functions to regular expressions, you’ll find a lot of topics throughout this membership site.

All the topics mentioned below are covered through complete examples.

Read a file as a chomped array

You can read a file in an array in a single line of code and chomp the array globally. This is not very suitable for a large file because the whole file is read into the array and the array is chomped only afterwards.

chomp the array elements one by one

There are more approaches to chomp the elements of an array one by one. The three examples provided in this membership site show you how to chomp the array elements one by one using foreach, while and map.

chomp a hash

If you remember, the Perl chomp function doesn’t act on the hash keys, only the values are chomped. You will find a few examples that help you chomp the keys of a hash.

chomp and $/

By default, the $/ is set to the newline character, i.e. "\n". You can play with the input record separator to perform different tasks, as follows:

  • slurp a file into one scalar variable – by setting $/ to undef
  • read a text file in paragraph mode – by setting $/ to ""
  • read a binary file – by setting $/ to "\0" character;

chomp a reference variable

You can use chomp with scalar, array or hash references. For example, to chomp an array reference you must first dereference the reference by using the notation @{$arrayRef}. You will get a complete example for each case as well as a few other ways to chomp the keys of a hash when a hash is defined by a reference.

Using chomp when reading from STDIN

Find out how to use chomp in connection with the diamond operator <>. You’ll see how to chomp text files either by supplying their names in the command line or by setting the elements of the @ARGV special variable inside a Perl script.

chomp in connection with the while and foreach statements

There are many approaches to reading some lines from STDIN and then chomping their ending newlines. I exemplify by using the foreach and while loop statements.

chomp and the loop controls

In order to change the loop behavior, the Perl language provides three loop control operators: last, next and redo. I provide an example about how you can use them in conjunction with the Perl chomp function.

chomp large data

If you have a file with large amount of data you can’t simply slurp the file in an array and globally chomp it because this can eat a big chunk of memory and you could run into some memory issues. A way to consider is using the while loop statement and I give you a complete example about how to achieve this.

chomp and push

You can use the push function if you need to read some lines one by one from STDIN, chomp and store them in an array. You can see how to implement it in a snippet script.

chomp and split

In this membership site I show you an example about how to use the chomp and split functions to read a csv file line by line, chomp each line and finally split the content of the line into different variables. Please remember that a csv file is a comma delimited text data file that has the extension 'csv' and there are a lot of applications such as Excel and MS Access that allow you to save the data in a csv format.

chomp multidimensional arrays or hashes

Please note that in Perl there are no such things as multidimensional arrays or hashes. All Perl arrays and hashes are internally one-dimensional. They can only store scalar values, meaning strings, numbers or references.

They cannot contain other arrays or hashes, but can contain references to other arrays or hashes. Using the mechanism of references, you can easily emulate multidimensional structures in Perl.

The particular cases are array of arrays, array of hashes, hash of hashes and hash of arrays.

Using the references model, you can build an array of arrays of hashes and so on; it depends on your needs.

In this part of the membership site I give you an example where I use a recursive subroutine to go through complex data structures and individually chomp those elements that are not references. The subroutine has a scalar value as its single argument. If an array/hash reference is found, the subroutine is invoked again for all the elements of the array/hash, using the foreach loop statement. If the argument is not a reference, the corresponding value will be chomped.

To avoid an infinite loop, the subroutine checks if the references are unique – if a reference appears in the complex data structure more than once, the script will print the respective reference and it will stop.

To check if a reference is unique I use a hash where I store the references as hash keys.

Obviously, you can use this subroutine to perform whatever you want instead of chomp by modifying the code appropriately.

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order

return from Perl chomp function to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!