Perl Tutorial : Perl data types
Perl Tutorial : Perl Operators
Perl Tutorial : Perl loops
Perl Tutorial : Perl Conditionals
Perl Tutorial : Perl Subroutines
Perl Tutorial : Perl regex
Perl Tutorial : Perl Files
Perl Tutorial : Perl & Databases
Perl Tutorial : Perl OOPs
Perl Tutorial : Perl signals
Perl Tutorial : Perl threads
Perl Tutorial : Perl Debugging

Introcution to Perl CGI

Introduction to modperl
Home >> perlfunctions >>

Perl function: chop

Syntax:
chop VARIABLE
chop (ARRAY)
chop
Removes the last character from the string no matter what that character is and returns that chopped character.

Chopping a string:
The last character of the string is chopped and this chopped character is returned. If the string is empty, chop returns empty string. If the chop function is applied on undefined string, same will be returned by chop.
Example:

$str = 'oranges';
print "Original string is: $str\n";
char = chop($str);
print "Chopped string is: $str\n";
print "Chopped character is: $char";
Output:
Original string is: oranges
Chopped string is: orange
Chopped character is: s
Chopping an array:
Last character of all the elements of the array is removed but the last character of the last element is returned. Only one dimensional arrays and hash without any reference can be chopped.
Example:
#!/usr/bin/perl
use strict;
use warnings;

@fruits = qw(oranges, grapes, apples);
$char = chop(@fruits);
print join(" ",@fruits), "\n"; 
print $char;
Output:
orange grape apple
s
Chopping hash:
Chopping hash removes the last character from all the values while the keys remain the same.
Example:
%fruits = (
     fruit1 => 'oranges',
     fruit2 => 'grapes',
     fruit3 => 'apples'
);
char = chop(%fruits);
while (($key, $value) = each % fruits)
{
  print "$key : $fruits{$key}\n";
}
print $char;
Output:
fruit3 : apple
fruit2 : grape
fruit1 : orange
s
Chopping empty:
When the VARIABLE or LIST is omitted from chop function, it chops the value stored in $_.
Example:
@fruits = qw(oranges grapes apples);
foreach (@fruits) {
  chop;
}
print "@fruits\n";
Output:
orange
grape
apple

Perl functions

scalars and strings

lc
ord

regex (regular expressions)

arrays

shift
split

More..

eval
Share |

List of all perl functions
Perl function : split
Perl function: abs
Perl function: chomp
Perl function: chr
Perl function: crypt
Perl function: eval
Perl function: hex
Perl function: lc
Perl function: lcfirst
Perl function: length
Perl function: oct
Perl function: ord
Perl function: shift

Perl examples

  • How to use Data::Dumper?
  • How to parse config file?
  • Include external module in perl..
  • Download perl

    download stable version of perl
    Perl 5.10.1
    Perl interview Questions
    sed tutorial
    awk tutorial

    Join us on Google Groups

    Subscribe to perl
    Email:
    Visit this group
    Perl Jobs


    privacy | sitemap | disclaim | contact us
    © 2009 perlhome.com