Friday, September 30, 2011

Perl function

split, join
$words = "the cat is white";
($w1, $w2, $w3, $w4) = split(/ /,$words);

结果是: the cat is white;
split可以写成这个样: (@words) = split(/ /, $words);

Join
$joined = join(" ", ("A", "black", "dog"));
print "$joined\n";
结果: A black dog #有


lc, uc, lcfirst, ucfirst

$words = "nz";
$words = lc($words);
all lower case,都要小写;

lcfirst第一个字母小写
$words = "ADADSFS";
$words = lcfirst($words);
print $words; 结果是aDADSFS;

length, substr, index, rindex

length("thursday"); 结果是8;

$string = "It was hot on thursday.";
substr($string, 14, 6);
从第14个字母开始(从0数),6个字母
结果是thursd;

substr($string, 14, 8, "tuesday");
结果是It was hot on tuesday.

$string = "There, here & everywhere";
$index = index($string, "here");
print "First 'here', position $index\n";
结果是: First 'here', position 1

rindex是last position;
$string = "There, here & everywhere";


No comments: