Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





chop Function in Perl


Tutorials Perl

Topic

What is chop Function in Perl?



Explanation
chop Function:

This is used to remove last character from the end of a string, or from end of every element of an array, or from every value of a hash.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $string = "anderson";
    $chr = chop($string);
    print $chr;
    print"<br>";
    @arr= ("sam", "jill", "fred");
    chop (@arr);
    print "@arr\n";
Result:
    n
    sa jil fre 

In the above example, last character from "anderson" is removed, and at the same time the deleted character is stored in the variable "$chr" and printed. In the second example last character of the array elements "@arr" is removed and displayed.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    %name = ('Tom',26,'Peter',51,'Jones', 23, 'John', 43);
    $chr = chop(%name);
    foreach $key (keys %name) 
    {
      print "$key: $name{$key}<br/>";
    }
Result:
   John: 4
   Jones: 2
   Peter: 5
   Tom: 2

In the above Perl example just the last character from every 'value' of each hash element is removed, then the hash is displayed.






A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting