- Web Development Tutorials

Chop, Chomp, Length, and Substring

How to use a few string functions in Perl
There are a few functions to manipulate strings that we haven't covered yet, so this will cover these as they may be useful later when we deal with strings even more. Let's take a look at the chop, chomp, length, and substring functions and see what they do.

Chop & Chomp

The chop function is used to "chop off" the last character of a string variable. It will remove that last character no matter what it is, so it should be used with caution. For example:


If you had read the "me" line in and assigned it to a variable, say $who_am _I, the value you have for it should be:


The chop command would look like this (assuming we assigned "me\n" to a variable named $who_am_I):

chop ($who_am_I);

Using the chop function in this case will remove the \n character. However, suppose we use it on the last of the three:


The "you" is the last piece of text in the file, and could be missing the newline \n character if it was, for instance, typed into the file manually and the "Enter" key was not pressed afterward. If the chop command is used in such a case, it will remove the "u", which was not intended! So code such as:

chop ($who_are_you);
print "You are $who_are_you!";

This would result in the viewer seeing "You are yo!" rather than the expected result. Thus, we will look at the chomp function.

The chomp function will remove the last character of a string, but only if that character is an input record separator (the current value of $/ in Perl), which defaults to the newline (\n) character. This is often used to remove the \n character when reading from a file. The chomp function is much safer than the chop function for this, as it will not remove the last character if it is not \n.

Now if we run the chomp command on that last line instead, it won't remove the "u".

chomp ($who_are_you);
print "You are $who_are_you!";

Now the viewer will see "You are you!" even if there was no \n character at the end of the "you" line.

Remembering these commands later will be very helpful when you read from files and need a specific string value.


The length function simply gives you back the number of characters in a string variable. This is handy when you don't know the value of the variable but would like to know the number of characters it has. It is useful with arrays, conditional statements, loops, and such things.

So, if you had a variable named $ice and its value was the string "cold", you could get the length of the string "cold" with the length function:

$length_ice = length ($ice);

Since the string "cold" has 4 characters, the value of the $length_ice variable will be 4, and you can use it for other calculations or conditions.

Substring (substr)

The substring function is a way to get a portion of a string value, rather than using the entire value. The value can then be used in a loop or a conditional statement, or just for its own purposes. For this one, you will probably want the general form of the function first. The function is usually set to a variable so that the variable contains the value of the substring:

$portion = substr($string_variable, start number, length);

Your $string_variable will be the variable from which you wish to create the substring. The start number is the character within the string from which you want to start your substring. Remember, though- the first number in a string here is zero rather than 1, so be careful when you make the count. The length above is the amount of characters you wish to take out of the string.

So, if we had a variable named $ice with a value of "cold", but we wanted to get the last three characters rather than the full string, we would write something like this:

$age = substr($ice, 1, 3);
print "It sure is $ice out here today.";
print "I wonder if I am $age enough to play in the snow?";

Yes, the substring turns out to be "old". It starts at the second character (which is 1 since the string starts with zero), and uses the next 3 characters. This function can be quite handy when you are trying to get part of a string later.

Well, that's it for now. Let's go on to: Reading from Files.

Copyright © The Web Design Resource. All rights reserved. | Contact Us | Privacy Policy