Perl列表及其类型解析和用法

2021年3月10日15:51:58 发表评论 637 次浏览

列表介绍

列表是标量值的集合。我们可以使用索引访问列表的元素。索引从0开始(第0个索引指的是列表的第一个元素)。我们使用括号和逗号运算符来构建列表。在Perl中, 标量变量以$符号开头, 而列表变量以@符号开头。

重要的提示 :perl中的List不是数据结构。它们只是代码中的一些子表达式/表达式。通常将它们分配给数组。

#!/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);

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

简单列表:

具有相同数据类型的列表称为简单列表

范例:

#!/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

复杂列表:

列表可能包含各种不同的数据类型。这些类型的列表称为复杂列表。

范例:

#!/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会将内部列表展平, 并且该列表的元素将被视为外部列表的元素。

范例:

#!/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中的标量变量是使用$符号访问的。

范例:

#!/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中切片列表可以通过将逗号分隔的索引值赋予另一个列表来完成。

例子:

#!/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, 则它将创建一个空列表, 否则将连续分配从leftValue到rightValue的值。

例子 :

#!/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" );
  
# Priting 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

组合范围和切片:

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

例子:

#!/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

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: