Perl

PERL Chomp Function

“The chomp() function is a built-in function of PERL to remove the newline(\n) character from the end of the string and the number of newline characters removed by this function. This function can be used in the script when it requires reading the string data after removing the newline character. The particular string value can be removed from the text by using the chomp() function that is stored in the $/ ($INPUT_RECORD_SEPARATOR). PERL has another built-in function named chop() that removes the last character of the string data, which can be a newline character or any other character. The uses of the chomp() function and the differences between the chomp() and chop() functions have been shown in this tutorial.”

Syntax

Different syntaxes of the chomp() function are shown below.

1. chomp

The above syntax is used to remove the newline from the value of the $_ variable.

2. chomp (String)

The above syntax is used to remove the newline from the string variable.

3. chomp (Array)

The above syntax is used to remove the newline from the array variable.

This function returns the number of newlines removed from the scalar variable or string, or array. Different uses of the chomp() function have been shown in the next part of the tutorial.

Example-1: Remove Newline From the Scalar Variable

Create a PERL file with the following code that will remove the newline from the scalar variable. A string value has been defined in the scalar variable here. The chomp() function has been used without any argument in this code to remove the newline from the scalar variable. The value of the scalar variable has been printed before and after using the chomp() function.

print "The value of the scalar variable before chomp():\n";

# Define the special variable $_

$_ = "LinuxHint\n";

# Print the $_ before removing newline

print;

# Remove the newline from the value of $_

$total = chomp;

# Print the total number of removed characters

print "Total character removed: $total\n";

print "The value of the scalar variable after chomp():\n";

# Print the $_ after removing newline

print;

Output

The following output will appear after executing the above code.

Example-2: Remove Newline From the String Variable

Create a PERL file with the following code that will remove the newline from the string value. A string variable with 2 newline characters has been declared in the code. The first chomp() function will remove the last newline character, and the second chomp() function will remove the next newline character. The third chomp() function will remove nothing.

# Define a string variable with two newline characters

$str = "PERL Programming\n\n";

 

# Remove the newline from the string if exists

$num  = chomp($str);

print "The String value after removing newline : $str\n";

# Print the total number of removed character

print "Total removed character(s) : $num\n";

 

# Remove the newline from the string if exists

$num  = chomp($str);

print "The String value after removing newline : $str\n";

# Print the total number of removed character

print "Total removed character(s) : $num\n";

 

# Remove the newline from the string if exists

$num  = chomp($str);

print "The String value after removing newline : $str\n";

# Print the total number of removed character

print "Total removed character(s) : $num\n";

Output

The following output will appear after executing the above code.

Example-3: Remove Newline From the Multiple Variables

Create a PERL file with the following code that will remove the newline from the multiple variables. Two string variables with newline have been defined in the code. Next, the chomp() has been used in the code to remove the newlines from both variables. The output of the chomp() function has been printed later.

# Define the multiple string variables

$language = "PERL\n";

$website = "linuxhint.com\n";

# use the chomp function for multiple variables

$output = chomp($language, $website);

# Print the chopped variables with other string data

print "Learn $language from $website\n";

# Print the value returned by the chomp function

print "Total number of newline removed: $output\n";

Output

The following output will appear after executing the above code.

Example-4: Remove the Particular String From Another String

The chomp() can be used to remove a particular character or string by defining the Input Record Separator(IRS) variable. Create a PERL file with the following code that will remove the word “hint” from a string variable. A string variable has been defined in the code that contains the word “hint” 3 times at the end of the string. The IRS variable has been used here to store the word “hint.” Next, the chomp() function was called 3 times to remove the word “hint” from the main string.

# Define a string variable

$str = "Learn PERL from Linuxhinthinthint";

# Set the input record separator

$/ = 'hint';

# Print the string variable before using chomp

print $str, "\n";

chomp $str;

# Print the string variable after applying chomp for the first time

print $str, "\n";

chomp $str;

# Print the string variable after applying chomp for the second time

print $str, "\n";

chomp $str;

# Print the string variable after applying chomp for the third time

print $str, "\n";       

Output

The following output will appear after executing the above code.

Example-5: Remove a Newline From Each Array Value

Create a PERL file with the following code that will remove the newline from each value of an array. The chomp() function has been used with the array to remove the newline from the array values. The array values before and after removing the newline and the output of the chomp() function will be printed after executing the code.

# Define an array of string values with newline

$flowers = ["Rose\n", "Lily\n", "Sun Flower\n", "Cosmos\n", "Daisy\n"];

 

# Print the main array values

print "The array values before using chomp:\n @$flowers\n";

 

# Remove newline from each array value

$output = chomp @$flowers;

 

# Print the output of the chomp function

print "Total newline removed: $output\n";

# Print the modified array values

print "The array values after using chomp:\n @$flowers\n";       

Output

The following output will appear after executing the above code.

Conclusion

The way of using the chomp() function for removing the newline or the particular string from the variable have shown here by using multiple PERL examples. I hope this tutorial will help the PERL users to know the use of this function properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.