📜  Perl – 数组与列表

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

Perl – 数组与列表

Perl 是一种通用的、解释的、动态的编程语言。 Perl 具有三种基本数据类型,即标量、数组和散列。

Perl 列表

该列表是一系列标量值。但是,列表不是 Perl 中的数据结构。在 Perl 中可以对列表执行有限的操作。由于没有变量引用此列表,因此列表不能用于打印以外的操作。

例子:

(10, 20, 30);
("this", "is", "a", "list", "in", "perl");

简单列表

简单列表是包含同类元素的列表。

# declaration without variable referencing
print("List declared and printed: ");
print join(' ', 10, 20, 30, 40, 50);
print("\n\n");
  
# qw() forms list by extracting words 
# out of the string using space as a delimiter.
print("List declared using qw(): ");
print join(' ', qw(this is gfg));
print("\n\n");
  
# indexing
print("Accessing element at index 2: ");
print((10, 20, 30, 40, 50)[2]);
print("\n\n");
  
# range
print("Range function on list\n");
print join(' ', 1..6);
print("\n\n");
  
# loop
print("Iterating over list elements:\n ");
foreach $element (1..6)
{
    print("$element\t");
}
print("\n\n");
  
# splicing
print("Splicing list\n");
print("Spliced elements: ");
print join(' ', (1..6)[1..3]);
print("\n\n");

输出:

List declared and printed: 10 20 30 40 50

List declared using qw(): this is gfg

Accessing element at index 2: 30

Range function on list
1 2 3 4 5 6

Iterating over list elements:
 1    2    3    4    5    6    

Splicing list
Spliced elements: 2 3 4

复杂列表

复杂列表是包含异构元素的列表。

print("complex", 10, 20, "list");

输出:

complex1020list

扁平化列表

如果存在嵌套列表,则将其合并为一个没有任何嵌套的列表。

print(2, 3, 4, (5, 6));
print("\n");
print(2, 3, 4, 5, 6);
print("\n");
print((2, 3, 4), 5, 6);
print("\n");

输出:

23456
23456
23456

Perl 数组

数组是一种 Perl 数据结构。 Perl 中的数组是一个包含列表的变量。数组变量以“@”符号为前缀。数组在 Perl 中有广泛的应用。可以对数组执行的操作类型没有限制。 Perl 中的数组可以是二维的,但列表不能是二维的。

例子:

@num = (10, 20, 30);
@str = ("this", "is", "a", "list", "in", "perl");

数组操作

# declaration
@array = (10, 20, 30, 40, 50);
print("Declared array\n");
print join(' ', @array);
print("\n\n");
  
# accessing particular element
print("Accessing element at index 2 \n");
print(@array[2]);
print("\n\n");
  
# push
print("Pushing two elements in to the array\n");
  
## returns total no. of elements in updated array
push(@array, (60, 70)); 
print join(' ', @array);
print("\n\n");
  
# pop
print("Popping elements from array\n");
print("Popped element: ");
  
## returns the popped elements of the array
print(pop(@array)); 
print("\n");
print join(' ', @array);
print("\n\n");
  
# shift
print("Shift element in an array\n");
shift(@array);
print join(' ', @array);
print("\n\n");
  
# unshift
print("Unshift element in an array\n");
unshift(@array, 10);
print join(' ', @array);
print("\n\n");
  
# splicing the array
print("Splice an array\n");
print("Spliced elements: ");
print join(' ', splice @array, 3, 2 );
print("\nArray after being spliced: ");
print join(' ', @array);
print("\n\n");
  
# reversing the array
print("Reverse elements in an array\n");
print join(' ', reverse @array);
print("\n\n");
  
# loop
print("Iterate over elements in an array\n");
foreach $element (@array)
{
    print("$element\t");
}
print("\n\n");
  
# range
print("Range function\n");
@array1 = (1..5);
print("@array1\t");
print("\n\n");

输出:

Declared array
10 20 30 40 50

Accessing element at index 2 
30

Pushing two elements in to the array
10 20 30 40 50 60 70

Popping elements from array
Popped element: 70
10 20 30 40 50 60

Shift element in an array
20 30 40 50 60

Unshift element in an array
10 20 30 40 50 60

Splice an array
Spliced elements: 40 50
Array after being spliced: 10 20 30 60

Reverse elements in an array
60 30 20 10

Iterate over elements in an array
10    20    30    60    

Range function
1 2 3 4 5    

二维阵列

Perl 允许创建二维数组。

@array = ( [ 10, 20, 30 ],
           [ "ana", "joe", "ester" ],
           [ "Welcome to gfg" ] );
  
for $array_ref( @array ) 
{
    print("[ @$array_ref ], \n");
}

输出:

[ 10 20 30 ],
[ ana joe ester ],
[ Welcome to gfg ],

下面是数组和列表之间的差异表:

Based onArraysLists
DeclarationA variable references a listNo variable references
Accessing elementsIndexing supportedIndexing supported
RangeRange supportedRange supported
PushPush is supported and newly added element is inserted at the end of the list.Push on list is forbidden.
PopPop is supported and an element is popped from the end of the list.Pop is forbidden on list
LoopArray elements can be iterated over using loopsList elements can be iterated over using loops
ShiftShift is supported and first element of the array is removedShift is forbidden on list
UnshiftUnshift is supported and adds the element to the front of the array.Unshift is forbidden on list
SpliceSplicing is supported in array.Splicing is supported in list.However, the spliced list cannot be accessed as no variable references it.
ReverseArray supports reversal operationList does not support reversal

在 Perl 中,列表和数组通常被认为是相同的。但两者之间存在差异。虽然列表不是 Perl 中的数据结构,但数组是引用 Perl 中列表的变量。在实际应用中,我们使用数组。