perl-basic-数据类型&引用

我觉得这一系列的标题应该是: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";

  

  

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值