perl 变量语法(标量和数组)
$salar, 标量, @array 数组
类型 | 字符 | 例子 | 用于哪种名字 |
标量 | $ | $cents | 一个独立的数值(数字或字串) |
数组 | @ | @large | 一列数值,用编号做键字 |
散列 | % | %interest | 一组数值,用字串做键字 |
子过程 | & | &how | 一段可以调用的 Perl 代码 |
类型团 | * | *struck | 所有叫 struck 的东西 |
- $answer = 42; # 一个整数
- $pi = 3.14159265 # 一个"实"数
- $pet = "Camel"; # 字串
- $sign = "I ove my $pet"; # 双引号:代换的字串
- $cose = 'It cose $100'; # 单引号:不带代换的字串
- $thence = $whence; # 另一个变量的数值
- $exit = system("set/a 1/0 2>nul"); # 一条命令的数字状态
- $time = `echo %time%`; # 获取windows当前时间
根据环境自动转换类型
$camels = '123'; print $camels +1, "\n";
-----------------------
复数变量 --> 数组的使用:
@home = ("couch", "chair", "table", "stove"); print "@home\n"; ($a, $b, $c, $d, $e) = @home; printf "\$a = $a, \$b = $b, \$c = $c, \$d = $d, \$e = $e"; ## 输出: couch chair table stove $a = couch, $b = chair, $c = table, $d = stove, $e =
数组元素的获取
@home = ("couch", "chair", "table", "stove"); print "\$home[0] = $home[0], \$home[1] = $home[1]"; ## 输出: $home[0] = couch, $home[1] = chair
散列的使用
%longday = ( "Sun", "Sunday", "Mon", "Monday", "Tue", "Tuesday", "Wed", "Wednesday", "Thu", "Thursday", "Fri", "Friday", "Sat", "Saturday" ); # 另外一种写法: %longday = ( "Sun" => "Sunday", "Mon" => "Monday", "Tue" => "Tuesday", "Wed" => "Wednesday", "Thu" => "Thursday", "Fri" => "Friday", "Sat" => "Saturday", );
散列表元素的获取
#因为是获取标量,因此你在 longday 前面使用 $,而不是 %,% 代表整个散列. $longday{"Wed"}
散列表的修改
%longday = ( "Sun" => "Sunday", "Mon" => "Monday", "Tue" => "Tuesday", "Wed" => "Wednesday", "Thu" => "Thursday", "Fri" => "Friday", "Sat" => "Saturday", ); $longday{"Wed"} = "周3"; print "$longday{\"Wed\"}";
散列表的创建
$wife{"Jacob"} = ["Leah", "Rachel", "Bilhah", "Zilpah"]; # 相当于: $wife{"Jacob"}[0] = "Leah"; $wife{"Jacob"}[1] = "Rachel"; $wife{"Jacob"}[2] = "Bilhah"; $wife{"Jacob"}[3] = "Zilpah"; print $wife{"Jacob"}[0], ""; # 输出: Leah # ------------------- $kids_of_wife{"Jacob"} = { "Leah" => ["Reuben", "Simeon", "Levi", "Judah", "Issachar", "Zebulun"], "Rachel" => ["Joseph", "Benjamin"], "Bilhah" => ["Dan", "Naphtali"], "Zilpah" => ["Gad", "Asher"],}; print $kids_of_wife{"Jacob"}{"Leah"}[0], ""; # 输出: Reuben
chop($number = <STDIN>); # 输入数字并删除新行
另外一种写法:
$number = <STDIN>; # 输入数字
chop($number); # 删除新行
. 操作符 (.) 来完成字符串的串联,这样就不会跟数字的加号相混淆。
$a = 123; $b = 456; print $a + $b; # 打印 579 print $a . $b; # 打印 123456
x 操作符
$a = 50; print "-" x $a; # 输出: --------------------------------------------------
文件
open(SESAME, "filename") # 从现存文件中读取 open(SESAME, "<filename") # (一样的东西,明确地做) open(SESAME, ">filename") # 创建文件并写入 open(SESAME, ">>filename") # 附加在现存文件后面 open(SESAME, "| output-pipe-command") # 设置一个输出过滤器 open(SESAME, "input-pipe-command |") # 设置一个输入过滤器
自操作元素符:
$a *= 3; # 你可读成“用 3 乘 $a”。 # Perl 中大多数的双目操作符都可以这么使用,甚至有些你在 c 语言中不能使用的也可以在 Perl 使用: $line .= "\n"; # 给 $line 附加一个新行 $fill x=80; # 把字串变成自填充 80 遍 $val ||= "2"; # 如果 $val 不为真则把它设置为 2
# 词频统计 open(fd, "a.c"); while($line = <fd>){ foreach $word (split /[^A-Za-z]/, $line) { $count{$word}++; } } foreach $word (keys %count) { printf("%-16s %5s\n",$word, $count{$word}); } <>