Next, we’ll explain how to use Perl chop function in your scripts and show you short examples in order to clarify some implementation aspects. Samples of snippet code will be provided in order to highlight some particular aspects of using it in different contexts. This quick tutorial will help you understand its features better. The Perl chop function has three syntax forms:
chop VARIABLE
chop (LIST)
chop
If we use this first syntax form, Perl chop function removes the last character of a string variable and returns the chopped character, it doesn’t matter what it is. Please don’t mix this function with chomp, they sound similar, but they have slight different meaning – the chomp function deletes the last character of a string if it is only a trailing newline (usually "\n").For instance, the following code shows you how to chop the last character from a string variable:
$v = 'Flowers';
$r = chop($v);
print "$v (without $r)\n";
# displays Flower (without s)
If the string is empty or undefined, the Perl chop function will return an empty string respectively undefined.
You can use chop with anything that's a lvalue, including an assignment, as in the following example:
chop($color = 'reds');
print "$color\n";
#displays red;
Check my new How To Tutorial eBook (PDF format):to see a lot of fully commented examples that help you use the chop function in your scripts.
The second syntax form is for lists. You can chop off an array or a hash, but keep in mind that we have in view one-dimensional structures only, without any reference. In both cases – array or hash - chop function will return the last character of the last element chopped.Chopping an array will cause all the elements of the array to be chopped:
# define an array
@flowers = qw(roses tulips violets snowdrops);
chop(@flowers);
print join(" ",@flowers), "\n";
# displays rose tulip violet snowdrop
For hashes (associative arrays) chop will remove the last character from the values while the keys remain unchanged: #define a hash
%colors = (
red => 'roses',
yellow => 'tulips',
white => 'snowdrops'
);
chop(%colors);
while (($key, $value) = each % colors)
{
print "$key : $colors{$key}\n";
}
After running this code, you will get the output:
white : snowdrop
red : rose
yellow : tulip
The third syntax form of Perl chop function is that when the variable is omitted. In this case chop uses the values stored in the special variable $_.For instance, see the next code:
# define an array
@flowers = qw(roses tulips violets snowdrops);
# let’s iterate through its elements using foreach loop
foreach (@flowers) {
chop;
}
print "@flowers\n";
# displays rose tulip violet snowdrop
In the example above, foreach is used without variable, so $_ special variable is used by default. At each iteration:- the $_ variable is set with the current element of the list
- the string from $_ is chopped by its last character
Finally, we used a print form with double quotes in order to display the elements separated by space.
Check my new How To Tutorial eBook (PDF format):to see a lot of fully commented examples that help you use the chop function in your scripts.
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :