📜  Perl 列表及其类型

📅  最后修改于: 2022-05-13 01:55:20.231000             🧑  作者: Mango

Perl 列表及其类型

列表简介

列表是标量值的集合。我们可以使用索引访问列表的元素。索引从 0 开始(第 0 个索引是指列表的第一个元素)。我们使用括号和逗号运算符来构造一个列表。在 Perl 中,标量变量以 $ 符号开头,而列表变量以 @ 符号开头。
重要说明: perl 中的列表不是数据结构。它们只是代码中的一些子表达式/表达式。它们通常分配给一个数组。

Perl
#!/usr/bin/perl
 
# Empty List assigned to an array
# Note that the expression () is a
# list and "empty_list" variable is an
# array variable.
@empty_list = ();
 
# Note that the expression (1, 2, 3) is a
# list and "integer_list" variable is an
# array variable.
@integer_list = (1, 2, 3);


Perl
#!/usr/bin/perl
 
# Empty List assigned to an array
@empty_list = ();
 
# List of integers
@integer_list = (1, 2, 3);
 
# List of strings assigned to an array
@string_list = ("Geeks", "for", "Geeks");
 
print "Empty list: @empty_list\n";
print "Integer list: @integer_list\n";
print "String list: @string_list\n";


Perl
#!/usr/bin/perl
 
# List of strings and integers assigned to an array
@complex_list = (1, 2, "Geeks", "for", "Geeks");
 
# printing this List
print "Complex List: @complex_list";


Perl
#!/usr/bin/perl
 
# Defining Internal list as an array
@Internal_list = (5, 6, 7);
 
# Defining External list.
@External_list = (1, "Geeks", 3, "For", @Internal_list);
 
# Printing Flattening list
print "Printing list within list: @External_list";


Perl
#!/usr/bin/perl
 
# Defining a list
@List = (1, 2, 3, 4, 5);
 
# Accessing list element
print "Second element of List is: $List[2]";


Perl
#!/usr/bin/perl
 
# Defining 1st List
@list1 = (1, "Geeks", 3, "For", 5);
 
# Defining 2nd List
@list2 = @list1[1, 2, 4];
 
# Printing Sliced List
print "Sliced List: @list2";


Perl
#!/usr/bin/perl
 
# Defining list with range of a to j
@x = ("a".."j");
 
# Defining list with range of 1 to 15
@y = (1..15);
 
# Defining list with range of A to J
@z = ("A".."J");
 
# Printing these lists
print "List with elements from a to j: @x\n";
print "List with elements from 1 to 15: @y\n";
print "List with elements from A to J: @z";


Perl
#!/usr/bin/perl
 
# Defining a list of elements
@x = ("Geeks", 2, 3, "For", 5);
 
# Use of Range and slice operator
@z = @x[2..4];
 
# Printing the sliced List
print "Sliced List: @z";



列表有多种类型,如下所述:

  • 简单列表:具有相同数据类型的列表称为简单列表
    例子 :

Perl

#!/usr/bin/perl
 
# Empty List assigned to an array
@empty_list = ();
 
# List of integers
@integer_list = (1, 2, 3);
 
# List of strings assigned to an array
@string_list = ("Geeks", "for", "Geeks");
 
print "Empty list: @empty_list\n";
print "Integer list: @integer_list\n";
print "String list: @string_list\n";
  • 输出:
Empty list: 
Integer list: 1 2 3
String list: Geeks for Geeks
  • 复杂列表:列表可能包含各种不同的数据类型。这些类型的列表称为复杂列表。
    例子 :

Perl

#!/usr/bin/perl
 
# List of strings and integers assigned to an array
@complex_list = (1, 2, "Geeks", "for", "Geeks");
 
# printing this List
print "Complex List: @complex_list";
  • 输出:
Complex List: 1 2 Geeks for Geeks
  • 扁平化列表:一个列表中可能包含另一个列表,但 Perl 将扁平化内部列表,并且该列表的元素将被视为外部列表的元素。
    例子 :

Perl

#!/usr/bin/perl
 
# Defining Internal list as an array
@Internal_list = (5, 6, 7);
 
# Defining External list.
@External_list = (1, "Geeks", 3, "For", @Internal_list);
 
# Printing Flattening list
print "Printing list within list: @External_list";
  • 输出:
Printing list within list: 1 Geeks 3 For 5 6 7

访问列表元素

可以使用标量变量访问列表元素。访问 List 元素时,使用 $,因为在 Perl 中使用 $ 符号访问标量变量。
例子 :

Perl

#!/usr/bin/perl
 
# Defining a list
@List = (1, 2, 3, 4, 5);
 
# Accessing list element
print "Second element of List is: $List[2]";

输出:

Second element of List is: 3

切片列表

在 Perl 中对列表进行切片可以通过将逗号 (,) 分隔的索引值赋予另一个列表来完成。
例子:

Perl

#!/usr/bin/perl
 
# Defining 1st List
@list1 = (1, "Geeks", 3, "For", 5);
 
# Defining 2nd List
@list2 = @list1[1, 2, 4];
 
# Printing Sliced List
print "Sliced List: @list2";

输出:

Sliced List: Geeks 3 5

在列表中定义范围

Perl 中的范围运算符用作创建列表的快捷方式。与列表一起使用时,范围运算符简化了创建具有连续数字和字母序列的列表的过程。范围运算符也可用于对列表进行切片。

注意:如果 leftValue 大于 rightValue 那么它将创建一个空列表,否则它将连续分配从 leftValue 到 rightValue 的值。
例子 :

Perl

#!/usr/bin/perl
 
# Defining list with range of a to j
@x = ("a".."j");
 
# Defining list with range of 1 to 15
@y = (1..15);
 
# Defining list with range of A to J
@z = ("A".."J");
 
# Printing these lists
print "List with elements from a to j: @x\n";
print "List with elements from 1 to 15: @y\n";
print "List with elements from A to J: @z";

输出:

List with elements from a to j: a b c d e f g h i j
List with elements from 1 to 15: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
List with elements from A to J: A B C D E F G H I J

结合范围和切片:
范围和切片运算符可以组合在一起对列表执行切片操作。
例子:

Perl

#!/usr/bin/perl
 
# Defining a list of elements
@x = ("Geeks", 2, 3, "For", 5);
 
# Use of Range and slice operator
@z = @x[2..4];
 
# Printing the sliced List
print "Sliced List: @z";

输出:

Sliced List: 3 For 5