The chop function removes the last character in a scalar variable : chop chomp « String « Perl






The chop function removes the last character in a scalar variable

   

#The chop function removes the last character of each word in an array. 
#The chop function returns value is the character it chopped. 
#The chomp function was introduced in Perl 5 to remove the last character in a scalar variable and the last character of each word in an array only if that character is the newline.

#Using chomp protects you from inadvertently removing some character other than the newline.


print "What is your name? ";
$name = <STDIN>;
print "$name.\n";
chop($name);   # Removes the last character.
print "$name.\n\n";
chop($name);
print "$name has been chopped a little too much.\n";
print "What is your age?  ";
chomp($age=<STDIN>); # Removes the last character if it is the newline.
chomp($age);         
print "$age!\n";

   
    
    
  








Related examples in the same category

1.Difference between chomp and chop
2.Type four characters with chomp
3.Type four characters without chomp
4.chomp in a while statement
5.chomp matches the input line separator defined by the $/ system variable.
6.chop function deletes the character at the right end of the line of text
7.The chomp function is a safer version of chop
8.Chomp the pre-set character
9.Chomp: Last character removed only if a newline
10.Chop function in action
11.Chop: Removing the last character, regardless of what it is
12.Cleaning Up Typed Input