 |
| |
Perl chop Function
NEW!!!
Check my new resource: Perl How To Tutorial eBooks
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. 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 eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions 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.
Subscribe now to my FREE newsletter (regarding tips and tricks to manipulate multidimensional arrays and hashes in Perl) and you'll receive a link to download for FREE my Perl chomp Function Tutorial eBook (a $7.50 value), it's a bonus for you and I hope you'll enjoy it. You can see its Table of Contents here (if you subscribe to my free newsletter, you don't have to pay for this eBook or use your paypal account).
Don't forget to check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions 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 :
Perl How To Tutorial eBooks
Table of Contents:
A Perl Script Install Perl Running Perl Perl Data Types Perl Variables Perl Operators Perl Lists Perl Arrays Array Size Array Length Perl Hashes Perl Statements Perl if Perl unless Perl switch Perl while Perl do-while Perl until Perl do-until Perl for Perl foreach Built-in Perl Functions Functions by Category String Functions Array Functions Functions in alphabetical order chomp chop chr crypt defined grep hex index join lc lcfirst length map oct ord pack pop push qw reverse rindex scalar shift sort splice split substr uc undef unshift
return from Perl chop function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!


|
|