珀尔 |数据类型
数据类型指定有效 Perl 变量可以保存的数据类型。 Perl 是一种松散类型的语言。在 Perl 程序中使用时无需指定数据的类型。 Perl 解释器将根据数据本身的上下文选择类型。
Perl 中有 3 种数据类型,如下所示:
- 标量
- 数组
- 哈希(关联数组)
1.标量:它是一个单一的数据单元,可以是整数、浮点数、字符、字符串、段落或整个网页。要了解有关标量的更多信息,请参阅Perl 中的标量。
例子:
Perl
# Perl Program to demonstrate the
# Scalars data types
# An integer assignment
$age = 1;
# A string
$name = "ABC";
# A floating point
$salary = 21.5;
# displaying result
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
Perl
# Perl Program to demonstrate
# the Scalars operations
#!/usr/bin/perl
# Concatenates strings
$str = "GFG" . " is the best";
# adds two numbers
$num = 1 + 0;
# multiplies two numbers
$mul = 4 * 9;
# concatenates string and number
$mix = $str . $num;
# displaying result
print "str = $str\n";
print "num = $num\n";
print "mul = $mul\n";
print "mix = $mix\n";
Perl
# Perl Program to demonstrate
# the Arrays data type
#!/usr/bin/perl
# creation of arrays
@ages = (33, 31, 27);
@names = ("Geeks", "for", "Geeks");
# displaying result
print "\$ages[0] = $ages[0]\n";
print "\$ages[1] = $ages[1]\n";
print "\$ages[2] = $ages[2]\n";
print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n";
Perl
# Perl Program to demonstrate the
# Hashes data type
# Hashes
%data = ('GFG', 7, 'for', 4, 'Geeks', 11);
#displaying result
print "\$data{'CR'} = $data{'CR'}\n";
print "\$data{'Ramos'} = $data{'Ramos'}\n";
print "\$data{'Bale'} = $data{'Bale'}\n";
输出:
Age = 1
Name = ABC
Salary = 21.5
- 标量运算:可以对标量数据类型执行许多操作,例如加法、减法、乘法等。
例子:
Perl
# Perl Program to demonstrate
# the Scalars operations
#!/usr/bin/perl
# Concatenates strings
$str = "GFG" . " is the best";
# adds two numbers
$num = 1 + 0;
# multiplies two numbers
$mul = 4 * 9;
# concatenates string and number
$mix = $str . $num;
# displaying result
print "str = $str\n";
print "num = $num\n";
print "mul = $mul\n";
print "mix = $mix\n";
输出:
str = GFG is the best
num = 1
mul = 36
mix = GFG is the best1
2、数组:数组是以列表的形式存储相同数据类型的值的变量。要在 Perl 中声明一个数组,我们在变量名前使用“@”符号。
@age=(10, 20, 30)
它将创建一个包含值 10、20 和 30 的整数数组。要访问数组的单个元素,我们使用“$”符号。
$age[0]
它将产生输出为 10。要了解有关数组的更多信息,请参阅Perl 中的数组
例子:
Perl
# Perl Program to demonstrate
# the Arrays data type
#!/usr/bin/perl
# creation of arrays
@ages = (33, 31, 27);
@names = ("Geeks", "for", "Geeks");
# displaying result
print "\$ages[0] = $ages[0]\n";
print "\$ages[1] = $ages[1]\n";
print "\$ages[2] = $ages[2]\n";
print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n";
输出:
$ages[0] = 33
$ages[1] = 31
$ages[2] = 27
$names[0] = Geeks
$names[1] = for
$names[2] = Geeks
3. Hashes(Associative Arrays):是一组键值对。它也被称为关联数组。要在 Perl 中声明哈希,我们使用 '%' 符号。要访问特定值,我们使用“$”符号,后面是大括号中的键。
例子:
Perl
# Perl Program to demonstrate the
# Hashes data type
# Hashes
%data = ('GFG', 7, 'for', 4, 'Geeks', 11);
#displaying result
print "\$data{'CR'} = $data{'CR'}\n";
print "\$data{'Ramos'} = $data{'Ramos'}\n";
print "\$data{'Bale'} = $data{'Bale'}\n";
输出:
$data{'GFG'} = 7
$data{'for'} = 4
$data{'Geeks'} = 11