Updated April 4, 2023
Introduction to Perl chop() Function
Perl chop() function is used to remove the last character of the input string and return the chopped character as an output to the user. Chop function has removed the last character from each element from the list, expression, and $_ if we have not specified any value with chop function in Perl. This function is very useful and important to remove the last character from string and return the chopped character string as an output. This is a built-in function of Perl language we can use this function while working with string.
Syntax:
Below is the syntax of the chop function:
1. Chop (String); – Input string used with chop function.
2. Chop variable; – Chop variable which was used in the program.
3. Chop (list) – List of last element.
4. Chop
Below is the Parameter Description Syntax
- Chop: It is used to remove the last character from the input string, this function is very important and useful in Perl. We can use a chop function with string, variable, hashes, $_, arrays, and string variables. Chop function is efficient and more important than s/.$//s because the chop function does not copy or scans the input string.
- String: String is defined as input string which we have used to remove the last character from a string. We can use any string or string variable with the chop function.
- Variable: This is defined as the chop variable which we have used with chop function in Perl. A variable parameter is very important and useful in chop function.
- List: If we have used input string as a chopped list then each of the elements from the list will be chopped. It will return chopped value as output to the user.
How chop() Function Works in Perl?
Below is the working of the chop function:
1. Chop function is used to remove the last character of the input string. If we have used array as input string then chop function will remove the last character from each input element from the given array.
2. If we have used input string as hashes then chop function will work the same as arrays, but in hashes, only the hash value will be chopped or remove. The key to hashes will not change it will remain the same.
3. We can use a chop function with character string while using chop function with character string it will delete the last character of the input string.
4. If we have used input string as a chopped list then each of the elements from the list will be chopped. It will return chopped value as output to the user.
5. We can also use chop function with scalar variables, this function will also remove the last character of scalar variables.
6. Chop function in Perl is removed the last character from each element from the list, expression, and $_ if we have not specified any value with chop function.
7. Chop function is a built-in function of Perl language we can use this function while working with string.
8. Perl chop function is used to remove the last character of the input string and return the chopped character as an output to the user.
9. Perl chop function is very useful and important to remove the last character from string and return the chopped character string as an output.
Examples
Below are the examples.
Example #1
Chop function using character string:
- In the below example, we have used character string with chop function in Perl. Chop function will delete the last character from the character string.
- We have user input character string as a “Perl example”.
Code:
use strict;
use warnings;
# Using chop function and reverse in perl.
# Define variable name as Char_string and char_reverse
my $Char_string = " Perl example ";
my $char_reverse = reverse $Char_string;
# Use chop function in input string.
chop($char_reverse);
$Char_string = reverse $char_reverse;
#Print the input string.
print "$Char_string\n";
$Char_string = "Perl example";
$Char_string = substr $Char_string, 1;
print "$Char_string\n";
Output:
Example #2
Chop function using string variable:
- In the below example, we have used a string variable with chop function. Chop function will delete the last character from the string variables.
- We have used input character string as a “Greens”.
Code:
use strict;
use warnings;
# Use input string as "Greens" using chop function.
chop(my $color_chop = 'Greens');
# Display the result of input string using chop function.
print "$color_chop\n";
Output:
Example #3
Chop function using an array:
- In the below example, we have using an array with chop function in Perl. The last character from every element will be deleted from array using chop function in perl.
- We have used input array as “reds blues greens”.
Code:
use strict;
use warnings;
# Use input as array of multiple color using chop function.
my @chop_color = qw(reds blues greens);
chop(@chop_color);
# Display the result array using chop function.
print join(" ",@chop_color), "\n";
Output:
Example #4
Chop function using hashes:
- In the below example, we have using hashes with chop function in Perl. In hashes, only the hash value will be chopped or remove. The key to hashes will not change it will remains the same.
Code:
use strict;
use warnings;
#define a hashes for colors and flowers
my %Chop_colors = ( red_color_for => 'roses',
yellow_color_for => 'tulips',
white_color_for => 'snowdrops'
);
chop(%Chop_colors);
while (my ($key, $value) = each % Chop_colors) {
print "$key : $Chop_colors{$key}\n";
}
Output:
Advantages
- Chop function is used to remove the last character from the input string.
- Chop function will return chopped string as an output to the user.
- We can use chop function with an array, scalar variables, string, and hashes in Perl.
- Chop function will the same work as hashes and arrays but only hash values will have changed but the key remains the same in hashes.
- Chop function is very important and useful in Perl to remove the last character of the string.
Conclusion
Chop function in Perl has removed the last character from each element from the list, expression, and $_ if we have not specified any value with chop function in Perl. Chop function is a built-in function of Perl language we can use this function while working with string.
Recommended Articles
We hope that this EDUCBA information on “Perl chop()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.