Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1738919
  • 博文数量: 438
  • 博客积分: 9799
  • 博客等级: 中将
  • 技术积分: 6092
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-25 17:25
文章分类

全部博文(438)

文章存档

2019年(1)

2013年(8)

2012年(429)

分类: Python/Ruby

2012-03-26 09:16:23

简单变量有整型、浮点型和字符串。$varname用来表示一个变量:


  1. #整型
  2. $a = 23; #十进制
  3. $b = 037; #八进制
  4. $c = 0xff; #十六进制
  5. print "\$a = $a \$b = $b \$c = $c\n";

  6. #浮点型
  7. $f = 33.5;
  8. $g = 3.35e1;
  9. $h = 335e-1;
  10. print "\$f = $f \$g = $g \$h = $h\n";

  11. #字符串
  12. $s = "Hello";
  13. $u = "\137"; #八进制ASCII码
  14. $v = "\x6f"; #十六进制ACSII码
  15. $w = '$s and \$s #单引号不替换变量,不支持转义字符,也不支持注释
  16.  the secode line'; #还可以跨行
  17. print "\$s = $s \$u = $u \$v = $v \$w = $w\n";

程序输出:


  1. $a = 23 $b = 31 $c = 255
  2. $f = 33.5 $g = 33.5 $h = 33.5
  3. $s = Hello $u = _ $v = o $w = $s and \$s #单引号不替换变量,不支持转义字符,也不支持注释
  4. the secode line

从上面的程序可以看到双引号里的字符串支持转义字符,可以有替换变量,而单引号却不支持。值得注意的是,Perl里的整型其实是存放在浮点寄存器里的,被当成浮点数对待。

下表是支持的转义字符:

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

 

字符串也可以转换为数值:


  1. #字符串和数值的转换
  2. $string = "53";
  3. $number = 47;
  4. $result = $string + $number;
  5. print "\$result=$result\n"; # $result=100

  6. #字符串中开头的所有数字字符可以视为一个数值。
  7. $result = "hello" * 5;
  8. print "\$result=$result\n"; # $result=0
  9. $result = "12a34" + 1;
  10. print "\$result=$result\n"; # $result=13

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

yourtommy2012-03-27 23:16:31

认真的鱼123: 恩,学习了!.....

认真的鱼1232012-03-27 22:37:35

恩,学习了!