Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1456117
  • 博文数量: 408
  • 博客积分: 10036
  • 博客等级: 上将
  • 技术积分: 4440
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-06 13:57
文章分类

全部博文(408)

文章存档

2011年(1)

2010年(2)

2009年(1)

2008年(3)

2007年(7)

2006年(394)

我的朋友

分类:

2006-07-24 17:51:11

perl 5基础教程——数据类型

 简单数据(标量数莼虼苛渴?荩┦莗erl用于数据操作的最基本数据类型,可以是数字或字符串。简单变量必须以$开头。


1、整型 
   例:
   $x = 12345;
   if (1217 + 116 == 1333) {
   # statement block goes here
   }
  整型的限制:
   PERL实际上把整数存在你的计算机中的浮点寄存器中,所以实际上被当作浮点数看待。在多数计算机中,浮点寄存器可以存贮约16位数字,长于此的被丢弃。整数实为浮点数的特例。
2、8进制和16进制数
  8进制以0打头,16进制以0x打头。
  例:$var1 = 047; (等于十进制的39)
  $var2 = 0x1f; (等于十进制的31)

  如 11.4 、 -0.3 、.3 、 3. 、 54.1e+02 、 5.41e03
  浮点寄存器通常不能精确地存贮浮点数,从而产生误差,在运算和比较中要特别注意。指数的范围通常为-309到+308。
  例:

  #!/usr/local/bin/perl
  $value = 9.01e+21 + 0.01 - 9.01e+21;
  print ("first value is ", $value, "\n");
  $value = 9.01e+21 - 9.01e+21 + 0.01;
  print ("second value is ", $value, "\n");

  ---------------------------------------------------------

  $ program3_3
  first value is 0
  second value is 0.01

   字符串有两种形式:单引号字符串、双引号字符串

(一)双引号字符串
 1. 双引号内的字符串中支持简单变量替换,例如:
  $number = 11;
  $text = "This text contains the number $number.";
  则$text的内容为:"This text contains the number 11."

 2. 双引号内的字符串中支持转义字符
Table Escape sequences in strings.

Escape Sequence Description
\a Bell (beep)
\b Backspace
\cn The Ctrl+n character
\e Escape
\E Ends the effect of \L, \U or \Q
\f Form feed
\l Forces the next letter into lowercase
\L All following letters are lowercase
\n Newline
\r Carriage return
\Q Do not look for special pattern characters
\t Tab
\u Force next letter into uppercase
\U All following letters are uppercase
\v Vertical tab

 \L、\U、\Q功能可以由\E关闭掉,如:
 $a = "T\LHIS IS A \ESTRING"; # same as "This is a STRING"

 3. 要在字符串中包含双引号或反斜线,则在其前加一个反斜线,反斜线还可以取消变量替换,如:
  $res = "A quote \" and A backslash \\";
  $result = 14;
  print ("The value of \$result is $result.\n")的结果为:
  The value of $result is 14.

 4. 可用\nnn(8进制)或\xnn(16进制)来表示ASCII字符,如:
  $result = "\377"; # this is the character 255,or EOF
  $result = "\xff"; # this is also 255

(二)单引号字符串
  单引号字符串与双引号字符串有两个区别,一是没有变量替换功能,二是反斜线不支持转义字符,而只在包含单引号和反斜线时起作用。单引号另一个特性是可以跨多行,如:
  $text = 'This is two
  lines of text
  ';
  与下句等效:
  $text = "This is two\nlines of text\n";

四、字符串和数值的互相转换
  例1:
  $string = "43";
  $number = 28;
  $result = $string + $number; # $result = 71
  若字符串中含有非数字的字符,则从左起至第一个非数字的字符,如:
  $result = "hello" * 5; # $result = 0
  $result = "12a34" +1; # $result = 13

五、变量初始值
  在PERL中,所有的简单变量都有缺省初始值:"",即空字符。

阅读(1089) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~