Updated April 4, 2023
Definition of Perl chomp()
Perl chomp() is used to remove any of the new line characters from the end of any string, chomp function will return the number of characters removed from the input string. Chomp function is very important to remove the newline character from the end of any string. Chomp function is a safer version of the chop function that will be used to remove the trailing string of corresponding to the current value of $, it will return the total number of arguments from its removed character. Chomp function will also use to remove new line character from the end of the input string.
Syntax:
Below is the syntax of the chomp function is as follows.
Chomp (String);
Input string used with chomp function.
Chomp variable;
Chomp variable which was used in the program.
Chomp (list)
List of the last element.
Chomp
Parameter:
- Chomp: Chomp function is very important and useful to remove new line character from the end of any string. Chomp function is also used to remove new line character from the end of the input string. The main use of chomp function is to remove any of the new line characters from the end of any string.
- String: String is defined as input string which we have used to remove new line characters from a string. We can use any new string or string variable with the chomp function in perl.
- Variable: This is defined as the chomp variable which we have used with chomp function in perl. The variable parameter is very important and useful in chomp function.
- List: If we have used input string as a chomped list then the new line character of the string from the list will be chomped. It will return chomped value as output to the user.
How chomp() Function Works in Perl?
Below is the working of chomp function in perl is as follows.
- Chomp function is an alternative to a chop function. It will return the number of new line characters remove from the input string.
- Chomp function will return the number of new line characters removed from the input string.
- This function is used to remove any of the new line characters from the end of the string.
- Chomp function will return the total number of arguments from its removed character. Chomp function will also use to remove the new line character from the end of the input string.
- Chomp function is a safer version of the chop function that will be used to remove the trailing string of corresponding to the current value of $.
- There is basically two-parameter used with chomp function, string and variables are the two important parameters of chomp function.
- Variable in chomp function defined as chomp variable which we have used with chomp function in perl.
- String in chomp function defined as input string which we have used to remove new line character from a string. We can use any new string or string variable with the chomp function in perl.
- If the value of the string does not end with “\n” value is not changed. The below example shows that value is not changed when the string does not end with “\n”.
Example:
# Initializing a string in perl
$chomp_str = "perl chomp function";
# Calling the chomp() function
$chomp = chomp($chomp_str);
# Print the chomped string and remove trailing newline character
print "Chomped string : $chomp_str";
print "Characters removed: $chomp";
Output:
- It is also called an input record separator in the English module. Chomp function will return a total number of characters removed from all its string arguments.
- If we chomp a list of string, each element of the list will be chomped and the total number of removed characters will be returned.
- Chomp function is a built-in function of perl language. We can use the chomp function when we have to work with the string.
- In simple word chomp function in perl states that it will remove the last symbol “\n” which was passed argument from the function.
Examples
Below is the example of the chomp function in perl is as follows.
1. Chomp Function in Perl
The below example shows that chomp function in perl. We have using a string as the “chomp” function to define an example of chomp function in perl are as follows.
Code:
# initializing a string in perl using chomp function.
$chomp_string = "chomp function\n";
# Calling the chomp() function in perl.
$chomp_perl = chomp($chomp_string);
# Print the chomped string and remove trailing newline character
print "Chomped string in perl: $chomp_string\n";
print "Characters removed: $chomp_perl";
Output:
2. Using Two Input String
In the below example, we have using two input string to define Perl example are as follows.
Code:
# Initializing two string in perl using chomp function.
$chomp_string1 = "Chomp function\n\n";
$chomp_string2 = "perl chomp function\n";
# Calling the chomp() function in perl using two string
$two_champ = chomp($chomp_string1, $chomp_string2);
# Print the chomped string and remove trailing newline character
print "Chomped String is : $chomp_string1$chomp_string2\n";
print "Number of Characters removed : $two_champ\n";
Output:
In the above example, we have used two \n but it will remove only last new line character from the string. The second new line character is utilized in the output as shown in the above figure.
Advantages
Below are the advantages of chomp function in perl are as follows.
- Chomp function is used to remove new line character from the input string.
- Chomp function will return the chomped string as an output to the user.
- If the value of the string does not end with the “\n” value is not changed then the value will remain the same using chomp function in Perl.
- If the value of the string is ended with more number of new line character then it will delete only the last character.
- Chomp function is very important and useful in Perl to remove the new line character from a string.
Conclusion
Chomp function is important in Perl to remove new line character from the end of the string. If the value of the string does not end with “\n” value is not changed. If the variable string contains the more “\n” character then it will delete only the last character.
Recommended Articles
We hope that this EDUCBA information on “Perl chomp()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.