Perl初级教程 - 第三天


 

 

 

 

第一页:条件语句

Perl当然也支持if/then/else语句,下面是一个例子:

if ($a)
{
	print "The string is not empty\n";
}
else
{
	print "The string is empty\n";
}

记住,空字符串被认为是false,如果$a是字符串"0",结果将是"empty"。

条件语句中也可以使用elsif:

if (!$a)			# The ! is the not operator
{
	print "The string is empty\n";
}
elsif (length($a) == 1)		# If above fails, try this
{
	print "The string has one character\n";
}
elsif (length($a) == 2)		# If that fails, try this
{
	print "The string has two characters\n";
}
else				# Now, everything has failed
{
	print "The string has lots of characters\n";
}

注意:elsif中确实缺一个"e"。>>

Perl初级教程
第一页 条件语句
第二页 字符串匹配
第三页 替换和翻译

[第1天][第2天][第3天][第4天]

 



本文根据 网猴 相关文章改编,版权归原作者所有。