perl学习(2) 基本数据类型等

来源:互联网 发布:淘宝一淘官方网站 编辑:程序博客网 时间:2024/05/16 11:33

1.1.数字

所有数字格式内部一致,全部是double

7.25e45  == 7.25 * 1045

5.25

6.00

2+3              #2+35

5.1-2.4         #5.1-2.42.7

3*12            #3*1236

14/2             #14/27

10.2/0.3 #10.2/0.334

10/3             #通常是浮点除,3.33333… …

还提供了模数运算符(%)。10%3的值是10 除以3的余数。两个操作数首先变成它们对应的整数值,如10.5%3.2转换为10%3后再计算。

指数操作符,两个*号表示,如2**3,表示23 次方,等于8

1.2.字符串

长度无限

Perl 中NUL 字符没有特殊的含义。Perl 能计算长度,不用靠null 来判断字符串是否结束。

有两种类型的字符串:单引号字符串和双引号字符串 

单引号字符串中的\n不会被当作换行符来处理,其仅仅是两个字符\n。只有在反斜线(\)后面接的是\或单引号,其才会被当作特殊符号来处理。

双引号字符串和在其它语言类似 

连接符 .

重复符 x

“hello”. “world” #同于“helloworld”

“hello”. ‘’. “world”#同于“hello world”

‘hello world’. “\n” #同于“hello world\n”

“fred”x 3 # “fredfredfred”

“barney”x (4+1) # “barney”x 5, “barneybarneybarneybarneybarney”

5 x 4 #实际上是“5”x 4, “5555” 

字符串和数字转换:

大多数情况下,Perl 将在需要的时候自动在数字和字符串之间转换,完全依赖于标量值之间的的操作符

12”* “3将给出36

12fred34”* “3将给出36  //后面的非数字部分和前面的空格将被去掉

fred"将给出0           //当一个不含任何数字的字符串将别转换为0

 

1.3.变量

$+标示符(由字母,数字和下划线组成,但不能由数字开头)

$fred = 17; #17赋给变量$fred

$barney =‘hello’; #将五个字母的字符串‘hello’赋给$barney

$barney = $fred + 3;#$fred 的值加上三赋给$barney (20)

$barney= $barney*2;#将变量$barney2 再赋给$barney (40)

$fred+=5; #利用二元赋值操作符

$barney*=3;

$str .= “”’; #同上

 

1.4.print

print “hello world\n”; #输出hello world后接换行符

print “The answer is ”;

print 6 * 7;

print “.\n”;

print “The answer is ”,6*7, “.\n”;

当一个字符串由双引号括起来时,如果变量前没有反斜线,则变量会被其值内插,也就是说字符串中的标量变量将被其值替换。

不使用双引号也可以得到相同的结果。但使用双引号更方便些。

在$符号之前使用\符号,变量不会被内插。 

$mean = “brontosaurus steak”;

$barney = “fred ate a $meal”; #$barney现在是“fred ate a brontosaurus steak”

$barney = ‘fred ate a’. $meal; #同上

print “The name is \$fred .\n”; #打印出美圆符号变量不会被其值替换

 

变量名将是字符串中有意义的最长的那一个。

$what="brontosaurus stea";

print "fred ate $n $whats.\n";      //fred ate 3 .

print "fred ate $n ${what}s.\n"     //fred ate 3 brontosaurus steas. 

1.5.操作符

C 和Perl 中都有的操作符有相同的优先级;

对于数字的比较,Perl提供了<, <=, = =, >=, !=这些操作符

对于字符串比较,Perl有如下的一些有趣的字符串比较符:lt ,le ,eq ,ge, gt, ne

35 == 35.0 #true

‘35’eq‘35.0’ #false (按照字符串比较)

 

1.6.if

if($name gt ‘fred’){

print “‘$name’comes after ‘fred’in sorted order.\n”;

}else{

print “‘$name’does not come after ‘fred’.\n”;

print “Maybe it’s the same string, in fact.\n”;

}

Perl 不同于其它的一些语言,它没有Boolean 类型,但是可以使用if($is_bigger){… }if(!$is_bigger){… }

● 如果值为数字,0 是false;其余为真

● 如果值为字符串,则空串(‘’)为false;其余为真

● 如果值的类型既不是数字又不是字符串,则将其转换为数字或字符串后再利用上述规则

 

1.7.while

while 语句可以循环执行其内部的一块代码直到其条件非真.

#! /usr/bin/perl -W$num=1 ;while($num < 10){        print $num,"\t" ;        $num+=1;}print "\n" ;


输出1      2       3       4       5       6       7       8       9 

1.8.STDIN

行输入操作符<STDIN>,由换行符结束(return)

 

#! /usr/bin/perl -W$line = <STDIN>;if($line eq "\n"){        print "That was just a blank line!\n";}else{        print "That line of input was: $line";}


 

结果:That was just a blank line!

 

使用chomp函数去掉\n

$text = “a line of text\n”; #也可以由<STDIN>输入

chomp($text); #去掉换行符(\n)

 

 用在列表中

chomp (@lines = <STDIN>); #读入所有的行,不包括换行符

读入所有的行,并去掉换行符 

1.9.undef,defined

变量在第一次赋值前有一个特殊值undef如果使用时是数字,则表现为0如果是“字符串”,则表现为空串。

要分辨其是undef还是空串,可以使用defined函数,它将在为undef 时返回false,其余返回true

#! /usr/bin/perl$madonna = undef;if(!defined($madonna)){        print "madonna undef\n";}else{        print "madonna not undef\n";}


 

结果为:madonna undef

如果想声明自己的undef值,可以使用undef

原创粉丝点击