Perl chop Function



Next, we’ll explain how to use the 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


Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!


chop VARIABLE


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;


chop (LIST)


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

chop


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.

Exercises


Through these exercises you have the opportunity to try yourself to write some script code where you can use the Perl chop function. These exercises are completely covered in my Perl "Perl Scalar and String Functions - How To Tutorial" where I show you how to play with this important function in detail.
1. Give a short example about how to chop the first character of a string.
2. Give a short script example where you use chop with a string, array and hash variable.
3. Give a script example where you use chop (the form with $_) with arrays in foreach loop.


A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!


Check these how-to tutorial eBooks (PDF format):


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
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        chomp
        chop (more)
        chr
        crypt
        defined
        delete
        each
        exists
        grep
        hex
        index
        join
        keys
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        q
        qq
        qw
        reverse
        rindex
        scalar
        shift
        sort
        splice
        split
        sprintf
        substr
        tr
        uc
        ucfirst
        undef
        unpack
        unshift
        values

return from Perl chop function to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!