The repository contains a collection of different programming snippets to different programming languages from own developments as well as other projects
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
programming-examples/perl/String/The chop function removes t...

20 lines
772 B

#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";
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.
print "$age!\n";