Perl, Perl Tutorials, Tutorials

How chomp and chop function works in Perl

Sending
User Rating 5 (1 vote)
How chomp and chop function works in Perl

What is chomp and chop?
chomp()
The chomp() function will remove (usually) any new line character from the end of a string. The reason we say usually is that it actually removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a new line. It returns the total number of characters removed from all its arguments and If VARIABLE is omitted, it chomps $_

So, if $/ =”\t” then chomp would remove every tab instead of new line.

For more information on the $/ variable, try perldoc perlvar and see the entry for $/ and perldoc -f chomp
Note: if chomp doesn’t behave as it should by default, hint is to check record separator ($/)

chop()
Sometimes you will find you want to unconditionally remove the last character from a string. While you can easily do this with regular expressions, chop is more efficient. Check perldoc -f chop

The chop() function will remove the last character of a string (or group of strings) regardless of what that character is and return the last character chopped. In case of array it return the last character chopped of the last element of the array. Note, if you want to easily remove newlines or line separators see the chomp().

As chop return the last character chopped of the string , if you want it’s reverse i.e. return all characters except the last from the string you could use substr($string, 0, -1)

Chomp and chop, both functions can be applied on strings,array,hash. We would see it by three examples for each functions.

Example 1. Chomping a string
Most often you will use chomp() when reading data from a file or from a user. When reading user input from the standard input stream (STDIN) for instance, you get a newline character with each line of data. chomp() is really useful in this case because you do not need to write a regular expression and you do not need to worry about it removing needed characters.
When running the example below, using the enter key on a line by itself will exit the program.

#!/usr/bin/perl
use strict;
use warnings;
my $username = <STDIN>;
print "before chomp: $username";
chomp $username; #or chomp($username); or chomp (my $username =<STDIN>); in one line
print "After chomp: $username";

Output:

[Sanjeev@Alien Coders]$ perl chomp_examples.pl&lt;br /&gt;Alien Coders&lt;br /&gt;before chomp: Alien Coders&lt;br /&gt;After chomp: Alien Coders[Sanjeev@Alien Coders]$

First print got printed with new line which you entered while typing. Yes, it took new line (pressed enter) also as user input. Second line removed that new line, so second print is displayed along with shell on the same line.

Example 2. Chomping an array
If you chomp an array, it will remove  newline from the end of every element in the array:

 #!/usr/bin/perl
use strict;
use warnings;
my @array = ("sanjeev\n", "Jassi", "AlienCoders\n");
print "Before chomp:\n";
print "@array\n";
chomp(@array);
print "After chomp:\n";
print "@array\n";

Output:

<br>Before chomp: sanjeev Jassi AlienCoders<br>After chomp: sanjeev Jassi AlienCoders<br>

As you can see, the newlines have been removed from “sanjeev” and “AlienCoders”, but no characters have been removed from “Jassi”.

Example 3. Chomping a hash
If you pass a hash into chomp() function, it will remove newlines from every value (not key) of the hash. Remember key is always unique and string (internally). So, if you add new line in key then use that new line char also while using that key. Better to avoid such nasty thing:

 #!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my %hash = (
  "first" =&amp;amp;gt; "one\n",
  "second" =&amp;amp;gt; "two\n",
  "third" =&amp;amp;gt; "three\n",
 );
print "before chomp:\n";
print Dumper(\%hash);
chomp(%hash);
print "after chomp:\n";
print Dumper(\%hash);

Output:

before chomp:&lt;br /&gt;$VAR1 = {&lt;br /&gt;'first' =&amp;gt; 'one&lt;br /&gt;',&lt;br /&gt;'second' =&amp;gt; 'two&lt;br /&gt;',&lt;br /&gt;'third' =&amp;gt; 'three&lt;br /&gt;'&lt;br /&gt;};&lt;br /&gt;after chomp:&lt;br /&gt;$VAR1 = {&lt;br /&gt;'first' =&amp;gt; 'one',&lt;br /&gt;'second' =&amp;gt; 'two',&lt;br /&gt;'third' =&amp;gt; 'three'&lt;br /&gt;};

It clearly shows that how new line is effective before chomp and it looks nice after chomp.

Example 4. Chopping a string
The chop() function removes and returns the last character from the given string whatever it is. So don’t do any mistake by assuming that it removed new line from the user input with its first use. When you will use it second time it will again remove one more character from user input but chomp only and only removes new line (or whatever is stored in $/).

#!/usr/bin/perl
use strict;
use warnings;

my $string = "Perl";
my $char = chop($string); #to return the chopped character in a variable

print "String: $string\n";
print "Char: $char\n";

Output:

String: Per&lt;br /&gt;Char: l

Example 5. Chopping an array
If you pass the chop() function to an array, it will remove the last character from every element in the array.

#!/usr/bin/perl
use strict;
use warnings;

my @arr = ("Jassi", "Sanjeev", "Alien Coders");
my $last_char = chop(@arr);

print "@arr\n";
print "Last Char: $last_char\n"; #it will store last character of last element in the array

Output:

Jass Sanjee Alien Coder&lt;br /&gt;Last Char: s

Example 6. Chopping  a hash
If you pass a hash into chop() function , it will remove the last character from the values (not the keys) in the hash. For example:

 #!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my %hash = (
first =&amp;amp;gt; "one",
second =&amp;amp;gt; "two",
third =&amp;amp;gt; "three",
);

print "Before chop:\n";
print Dumper \%hash;

my $chr = chop(%hash);
print "After chop: \n";
print Dumper(\%hash);

print "Char: $chr\n"; #it always have last character of last value in hash

Output:

Before chop:&lt;br /&gt;$VAR1 = {&lt;br /&gt;'first' =&amp;gt; 'one',&lt;br /&gt;'second' =&amp;gt; 'two',&lt;br /&gt;'third' =&amp;gt; 'three'&lt;br /&gt;};&lt;br /&gt;After chop:&lt;br /&gt;$VAR1 = {&lt;br /&gt;'first' =&amp;gt; 'on',&lt;br /&gt;'second' =&amp;gt; 'tw',&lt;br /&gt;'third' =&amp;gt; 'thre'&lt;br /&gt;};&lt;br /&gt;Char: e

Note:

  • chop and chomp functions would not work on array/hash reference or array of array/hash reference. So, avoid using this on referential things.
  • Always use parenthesis, if you are using assignment operator while chomping or chopping.
    chomp $value = <STDIN>;  doesn’t work  but chomp($value=<STDIN>); works

Credit: http://perlmeme.org

Share your Thoughts