Perl chop Function

The Perl chop function is used to remove and return the last character from a string.

You can use the chop function to remove the last character of a string, regardless of whatever it is. If you call the chop on an array, it will remove the last character of each element of the array. It acts similar on hashes, but only the values will be chopped, while the keys remain unchanged.

Please don’t mix this function with chomp, they sound similar, but they have slight different meaning. While a chop function deletes the last ending character regardless of whatever it is, a chomp function checks whether the last character(s) match the input line separator $/ (by default it is set to the newline character, i. e. "\n") and only then deletes them.

You can use it with different arguments: scalar variables, lists or without any argument at all. It returns the last character removed.

There are three syntax forms for this function:

chop VARIABLE
chop (LIST)
chop
If you use the first syntax form, the Perl chop function removes the last character of the variable and returns the chopped character, it doesn’t matter whatever it is.

The second syntax form is for lists. You can chop off an array or a hash. Chopping an array will cause all the elements of the array to be chopped. For hashes, chop will remove the last character from all the values while the keys remain unchanged.

The third syntax form of the chop function is that when the variable is omitted. In this case chop uses the value stored in the special variable $_.

Well, the Perl chop function is meant to delete the last character of a string regardless of whatever it is. But if you insist, you could reverse, chop and reverse the string again in order to chop its first character.

I think a better idea is to use the substr function. See the example below for all these:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# Using chop and reverse
my $string = " Perl syntax";
my $reversed = reverse $string;
chop($reversed);
$string = reverse $reversed;
print "$string\n";
 
# Using substr
$string = " Perl syntax";
$string = substr $string, 1;
print "$string\n";
Output:
 
Perl syntax
Perl syntax

If you call the Perl chop function against a scalar string variable, this function will remove the last character of the string doesn’t matter what it is and it will return the chopped character. In the example above the chop function is used with an assignment:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
chop(my $color = 'reds');
print "$color\n";
# displays: red
 

To Perl chomp an array will cause all the elements of the array to be chopped and the last character of the last element of the array will be return. If through the elements of the array some are references to other structures, these elements will be remained unchopped.

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# define an array
my @flowers = qw(roses tulips violets snowdrops);
chop(@flowers);
print join(" ",@flowers), "\n";
# displays rose tulip violet snowdrop

For hashes (associative arrays), chop will remove the last character from all the values, while the keys remain unchanged. If through the values of the hash some are references, these elements will not be chopped.

We are illustrating this in the next example:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
#define a hash
my %colors = ( red => 'roses',
               yellow => 'tulips',
               white => 'snowdrops'
);
chop(%colors);
 
while (my ($key, $value) = each % colors) {
  print "$key : $colors{$key}\n";
}
Output:
 
white : snowdrop
red : rose
yellow : tulip

 

In the case the argument variable of the Perl chop function will be omitted, chop uses the value stored in the special variable $_.

For instance, see the next code:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# define an array
my @flowers = qw(roses tulips violets snowdrops);
 
# lets iterate through its elements using foreach loop
foreach (@flowers) {
  chop;
}
 
print "@flowers\n";
# displays rose tulip violet snowdrop
In the example above, foreach is used without the iterator variable, so the $_ special variable is used by default.

At each iteration step:

  • the $_ variable is set with the current element of the list
  • the string from $_ is chopped by its last character

Finally, we used a print form with double quotes in order to display the array elements separated by space.

Instead of foreach block you can use an one line statement:

chop foreach (@flowers);