码迷,mamicode.com
首页 > 编程语言 > 详细

Perl语言学习之变量

时间:2015-05-13 00:35:01      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

1.类型

  Perl只有三种类型,标准变量,数组和散列(哈希),分别用三种符号进行表示,即$,@,%。

#!/usr/bin/perl
$name = "xuzhang";
print "$name\n"; #xuzhang

@months = (xuzhang,mating,mao);

%person = (
    name => xuzhang,
    age => 18,
    adress => china
    );
print "@months\n"; #xuzhang mating mao
print "$months[0]\n"; #xuzhang
print "my name is $person{‘name‘} and my age is $person{‘age‘} and my adress is $person{‘adress‘}";
#my name is xuzhang and my age is 18 and my adress is china

   上面的代码展示perl的三种数据类型,数组的声明使用@,下标和其他的语言没什么区别,都是从0开始,$#arrayname表示最后一个下标的index,通常可以用

$#arrayname+1代表数组的长度。而散列的应用则是应用花括号,不是javascript中的[],这一点要注意的一下,最简单的就是这样了,但是一嵌套就千变万化了,祝各位好运。

2.引号规范

  Perl 拥有三种类型的引号 , 均提供不同的功能 。 它们分别是单引号 、 双引号和反引号 ( backquote )。

  单引号是一种 “ 民主 ” 的引号 。 它对位于其中的所有字符均一视同仁 。 换而言之 , 单引号不支持特殊字符 , 而双引号则会区别对待一般字符和特殊字符 。 后者会将字符串中的部分字符当作特殊字符对待 。 其特殊字符包括美元符号 $ 、 @ 符号以及转义序列 , 如 \t 和 \n 等 。当命令位于反引号中时 , 该命令将由 shell 执行。

#!/usr/bin/perl
$sum = 5;
print "The number is $sum.\n"; #$sum替换成5
print "I need \$5.00.\n";#转意符号
print "\t\tI can‘t help you.\n";

  单引号里面的特殊字符不进行转译。

  

技术分享
1 #!/usr/bin/perl
2 print "The date is ", `date`;#The date is 2015年 05月 12日 星期二 21:59:14 CST
3 $directory = `pwd`;
4 #the current directory is /home/xuzhang/shell/perl
5 print "the current directory is $directory
  Perl 的替换符号 。 Perl 提供了另一种表示引号的形式 —— 即 q 、 qq 、 qx 和 qw 结构 。

    1.q 代表一个单引号 。     q/Hello/  等同 ‘ Hello ’
    2.qq 代表一个双引号 。 qq/Hello/ 等同 “ Hello ” 
    3.qx 代表一个反引号 。 qx/date/ 等同 ‘ date ’
    4.qw 代表引用的字列表 。 @list=qw/red yellow blue/; 等同 @list={ ‘ red ’ , ‘ yellow ’ , ‘ blue ’ }

3.数组

  与 C 语言不同 , Perl 并不要求数组元素具有相同的数据类型 ,数组元素可以是数字和字符串的混合体。python,ruby,javascript这几种脚本语言都是一样的。

技术分享
 1 #!/usr/bin/perl
 2 @grades = (90,89,78,100,87);
 3 #The original array is: 90 89 78 100 87
 4 print "The original array is: @grades\n";
 5 
 6 print "The number of the last index is $#grades\n"; #4
 7 
 8 #改变了数组最后一个元素的下标
 9 $#grades=3;
10 
11 #The array is truncated to 4 elements: 90 89 78 100
12 print "The array is truncated to 4 elements: @grades\n";
13 
14 #empty the array
15 @grades=();
16 
17 print "The array is completely truncated: @grades\n";
18 
19 @digits=(0 .. 10); #0~10
20 print @digits;
21 @letters=( A .. Z ); #A~Z
22 @alpha=( A .. Z, a .. z );#A~Za~z
23 @n=( -5 .. 20 );#-5~20
View Code

 

 4.散列

  

技术分享
 1 #!/usr/bin/perl
 2 %department = (
 3 "Eng" => "Engineering",
 4 "M" => "Math",
 5 "S" => "Science",
 6 "CS" => "Computer Science",
 7 "Ed" => "Education",
 8 );
 9 
10 #get the value
11 print "$department{‘M‘}\n";
12 
13 #add the new key and value
14 
15 $department{xuzhang} = mating;
16 print $department{xuzhang}; #mating
17 
18 #hash slices
19 #散列分片 ( hash slices )。 所谓散列分片 , 是一组散列键的列表 , 并且这些键的关联值是另一
20 #组键的列表 。 该列表由多个散列名组成 , 并以 @ 符号开头 。 散列键的列表则位于花括号中 。
21 %officer= ("NAME"=> "Tom Savage",
22 "SSN" => "510-22-3456",
23 "DOB" => "05/19/66"
24 );
25 @info=qw(Marine Captain 50000);
26 @officer{BRANCH, TITLE, SALARY}=@info;
27 
28 foreach $key (NAME, SSN, DOB, BRANCH, TITLE, SALARY){
29     printf "Key: %-10sValue: %-15s\n", $key, $officer{$key};
30 }
31 # Key: SSN       Value: 510-22-3456    
32 # Key: DOB       Value: 05/19/66       
33 # Key: BRANCH    Value: Marine         
34 # Key: TITLE     Value: Captain        
35 # Key: SALARY    Value: 50000 
View Code

 

Perl语言学习之变量

标签:

原文地址:http://www.cnblogs.com/xz0636/p/4498697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!