Perl chop函数


PPPPPS:学习Perl语言编程中……,打算边学边把Perl函数整理出来。

介绍:

chop函数会砍掉字符串变量的最后一个字符,并返回砍掉的字符。chop函数不管字符串里是什么都会剪短它,而chomp函数则更有选择性一些。

不能chop直接量,只能chop变量。

用法:

chop VARIABLE 

chop LIST 

chop

例子:

如果chop一列LIST变量,那么列表中的每个字符串都会被剪短:

@lines = `cat myfile`;
chop @lines;

在最常见的情况下,chop可以用substr来表示:

$last_char = chop($var);
$last_char = substr($var, -1, 1, ""); # 同上

再来看下面的完整的例子:

#!/usr/bin/perl

$string1 = "This is test";
$retval  = chop( $string1 );

print " Choped String is : $string1\n";
print " Character removed : $retval\n";

结果:

 Choped String is : This is tes
 Number of characters removed : t

Perl chop函数 完~~


《 “Perl chop函数” 》 有 9 条评论