Perl 变量,perl 中有三种类型的变量:标量,标量和散列数组。让我们在示例的帮助下逐一学习它们。
标量
标量单一数据单元。标量可以是整数,浮点数,字符串等。标量变量以$
符号为前缀。让我们看看下面的 perl 脚本,其中我们有三个标量变量。
#!/usr/bin/perl
# Integer
age = 29;
# Stringname = "Chaitanya Singh";
# Float
height = 180.88;
print "Name:name\n";
print "Age: age\n";
print "Height:height\n";
输出:
Name: Chaitanya Singh
Age: 29
Height: 180.88
数组
数组是标量的有序列表,数组变量的前缀为@
符号,如下例所示:
#!/usr/bin/perl
@friends = ("Ajeet", "Leo", "Rahul", "Dhruv");
print "\friends[0] =friends[0]\n";
print "\friends[1] =friends[1]\n";
print "\friends[2] =friends[2]\n";
print "\friends[3] =friends[3]\n";
输出:
$friends[0] = Ajeet
$friends[1] = Leo
$friends[2] = Rahul
$friends[3] = Dhruv
哈希(也称为关联数组)
哈希是一组键值对。哈希变量以%
符号为前缀。让我们看看下面的例子:
#!/usr/bin/perl
%age = ('Chaitanya Singh', 29, 'Ajeet', 28, 'Lisa', 25);
print "\age{'Lisa'}:age{'Lisa'}\n";
print "\age{'Chaitanya Singh'}:age{'Chaitanya Singh'}\n";
print "\age{'Ajeet'}:age{'Ajeet'}\n";
输出:
$age{'Lisa'}: 25
$age{'Chaitanya Singh'}: 29
$age{'Ajeet'}: 28