我觉得这一系列的标题应该是:PERL,从入门到放弃

USE IT OR U WILL LOSE IT

参考资料:

https://qntm.org/files/perl/perl.html

在线perl编译器:

https://www.tutorialspoint.com/execute_perl_online.php


use strict;
use warnings;
# no block comments # variables: use 'my' for declaration
# 1. scalars: undef(like NULL), number, string, reference to any other variables
# no distinguish between float and int
my $num = 12.34;
# use '.' for string concatenation
my $str = "hello, "."m'lady";
print $str."\n";
# no boolean type. but "false" in condition means: 0,"","0",undef
# 2. arrays: use @variableName
# trailing comma is OK
my @arr = (1, "hi", 3,);
# use '$' because it's scala when retrived
print $arr[0]."\n";
# negative index means retriving from backend
print "$arr[-2]\n";
# '' will not interpret, just output raw string
# "" will interpret variables
# they're same: print 'Hello \$str \@arr'
print "Hello \$str \@arr" # OPERATORS
# Numerical operators: <, >, <=, >=, ==, !=, <=>, +, *
# String operators: lt, gt, le, ge, eq, ne, cmp, ., x

啦啦啦今天继续啊

数组长度:

my @arr = (2, "Hi", "my", "lady", "!");
# the length of array
print scalar @arr;
print "\n";
# the end index of array. aka, length-1
print $#arr;
  • hash Tables:
use strict;
use warnings; # hash variable
my %hashTable = ("1st" => "hi",
"2nd" => "my",
"3rd" => "lady");
# since some element is a scalar
print $hashTable{"1st"};
use strict;
use warnings; my %hashTable = (1=>"hi", 2=>"my", "3rd"=>"lady"); # convert to array
my @arr = %hashTable;
# 1hi2my3rdlady
print @arr;
# output 1
print $arr[0]; # 索引时hash用{},array[],""有无无所谓;构建时两者都用() 
#print $hashTable{1};
#print $hashTable{"1"};
  • list  

列表指的是用()构建的东西,array和hash都是。列表可以作为一个元素放在列表中间,但是会失去层次结构。hash的话,

use strict;
use warnings; my @array = (
"apples",
"bananas",
(
"inner",
"list",
"several",
"entries",
),
"cherries",
);
my $i = 0;
while ($i <= $#array) {
print "array[$i] is ".$array[$i]."\n";
$i = $i + 1;
}

但其实list与array还是不同的,如下例:

my @arr = ("alpha", "beta", "gamma", "pie");
# 这里得到arr的长度
my $num = @arr;
print "$num\n";
# output: pie $num1存的是最后一项
my $num1 = ("alpha", "beta", "gamma", "pie");
print "$num1\n";

Every expression in Perl is evaluated either in scalar context or list context

而对很多函数来说,对scalar和list的处理方式是不同的,如reverse:

# link. no reverse
print reverse "hello, my lady", 3, " ";
print "\n";
# reverse each letter
my $scalar = reverse "hello, my lady";
print "$scalar\n";

可以在前面强制加scalar,让函数可以按照scalar的方式处理:

print scalar reverse "hello world"; # "dlrow olleh"  

而在下面的例子中,给$outer[3]赋值时,由于它是scalar,所以取得的值只能是数组长度,而非数组。

my @outer = ("Sun", "Mercury", "Venus", undef, "Mars");
my @inner = ("Earth", "Moon"); $outer[3] = @inner; print $outer[3]; # "2"  

好吧,为了解决这个问题,使用引用:

my $colour    = "Indigo";
# add reference
my $scalarRef = \$colour;
print $colour; # "Indigo"
print $scalarRef; # e.g. "SCALAR(0x182c180)"
print ${ $scalarRef }; # "Indigo"
# 如果不会混淆,可以省略{}
print $$scalarRef; my @colours = ("Red", "Orange", "Yellow", "Green", "Blue");
my $arrayRef = \@colours; print $colours[0]; # direct array access
print ${ $arrayRef }[0]; # use the reference to get to the array
print $arrayRef->[0]; # exactly the same thing my %atomicWeights = ("Hydrogen" => 1.008, "Helium" => 4.003, "Manganese" => 54.94);
my $hashRef = \%atomicWeights; print $atomicWeights{"Helium"}; # direct hash access
print ${ $hashRef }{"Helium"}; # use a reference to get to the hash
print $hashRef->{"Helium"}; # exactly the same thing - this is very common

我们希望定义自己的数据类型,类似C++里类的概念,所以我们这样做:

# Braces denote an anonymous hash
my $owner1Ref = {
"name" => "Santa Claus",
"DOB" => "1882-12-25",
}; my $owner2Ref = {
"name" => "Mickey Mouse",
"DOB" => "1928-11-18",
}; # Square brackets denote an anonymous array
my $ownersRef = [ $owner1Ref, $owner2Ref ]; my %account = (
"number" => "12345678",
"opened" => "2000-01-01",
"owners" => $ownersRef,
);

注意,这里$owner1Ref和$owner2Ref存的其实是一个匿名hash的引用,$ownersRef存的则是匿名array的引用。既然时候引用,

它们的实际值就是地址。所以%account中才能够保存完整的信息。等价于:

my %account = (
"number" => "31415926",
"opened" => "3000-01-01",
"owners" => [
{
"name" => "Philip Fry",
"DOB" => "1974-08-06",
},
{
"name" => "Hubert Farnsworth",
"DOB" => "2841-04-09",
},
],
); 

如何打印?使用->

print "Account #", $account{"number"}, "\n";
print "Opened on ", $account{"opened"}, "\n";
print "Joint owners:\n";
print "\t", $account{"owners"}->[0]->{"name"}, " (born ", $account{"owners"}->[0]->{"DOB"}, ")\n";
print "\t", $account{"owners"}->[1]->{"name"}, " (born ", $account{"owners"}->[1]->{"DOB"}, ")\n";

  

  

  

最新文章

  1. Angularjs真是个好东西
  2. ORACLE中创建和删除临时表
  3. Spring和SpringMVC的区别
  4. sqlite3使用简介(内含解决sqlite内存的方法)
  5. Java对象的序列化与反序列化:默认格式及JSON格式(使用jackson)
  6. [有用命令]Linux 用户,用户组
  7. php之PDO使用【转载】
  8. Vue基础概念,学习环境等
  9. duilib消息类型
  10. C语言博客作业04——数组
  11. scrapy Mongodb 储存
  12. jquery 学习(六) - 事件绑定
  13. Javascript面向对象编程(三):非构造函数的继承(对象的深拷贝与浅拷贝)
  14. JS获取鼠标左(右)滑事件
  15. 创建文件指针数组c++
  16. C#6.0语言规范(十七) 特性
  17. python数据结构与算法 29-1 哈希查找
  18. Java 图片处理解决方案:ImageMagick 快速入门教程
  19. 象“[]”、“.”、“-&gt;”这类操作符前后不加空格
  20. python3高阶函数:map(),reduce(),filter()的区别

热门文章

  1. 服务器配置,负载均衡时需配置MachineKey
  2. Column &#39;xxx&#39; in field list is ambiguous
  3. 使用一条sql语句查询多表的总数
  4. WPF 模拟Button按钮事件触发
  5. 有关JSOUP学习分享(一)
  6. linux touch和vi建立的文件是什么文件类型的
  7. spring4、hibernate4整合xml配置
  8. 初学Vue.js(2.x版本)
  9. linux 查看dd进度
  10. SCCM Collection 集合获取计算机最后启动时间