Home >> perlfunctions >>Perl function: chopSyntax:chop VARIABLE chop (ARRAY) chopRemoves the last character from the string no matter what that character is and returns that chopped character.
Chopping a string: $str = 'oranges'; print "Original string is: $str\n"; char = chop($str); print "Chopped string is: $str\n"; print "Chopped character is: $char";Output: Original string is: oranges Chopped string is: orange Chopped character is: sChopping an array: Last character of all the elements of the array is removed but the last character of the last element is returned. Only one dimensional arrays and hash without any reference can be chopped. Example: #!/usr/bin/perl
use strict;
use warnings;
@fruits = qw(oranges, grapes, apples);
$char = chop(@fruits);
print join(" ",@fruits), "\n";
print $char;
Output:
orange grape apple sChopping hash: Chopping hash removes the last character from all the values while the keys remain the same. Example: %fruits = (
fruit1 => 'oranges',
fruit2 => 'grapes',
fruit3 => 'apples'
);
char = chop(%fruits);
while (($key, $value) = each % fruits)
{
print "$key : $fruits{$key}\n";
}
print $char;
Output:
fruit3 : apple fruit2 : grape fruit1 : orange sChopping empty: When the VARIABLE or LIST is omitted from chop function, it chops the value stored in $_. Example: @fruits = qw(oranges grapes apples);
foreach (@fruits) {
chop;
}
print "@fruits\n";
Output:
orange grape apple Perl functions
|
List of all perl functions Perl function : split Perl function: abs Perl function: chomp Perl function: chr Perl function: crypt Perl function: eval Perl function: hex Perl function: lc Perl function: lcfirst Perl function: length Perl function: oct Perl function: ord Perl function: shift
|