[Perl] Perl array method summary

                                         Perl array method summary

 

  • table of Contents

First, the declaration of arrays in Perl

Second, the initialization of arrays in Perl

2.1. Directly assign initial values

2.2. Use qw to assign values ​​to the array

2.3. Arithmetic sequence

Three, the basic method of the array

3.1. Access to array elements

3.2. Traverse the array with foreach

3.3. Append elements like an array

3.4. Delete the elements in the array

3.5. Sorting of array elements

3.6. Array empty

Fourth, the special member variables of the array

4.1. $ # Array represents the index of the last element of the array, the length of the array is -1

4.2. @Array represents the length of the array, and printing directly will output the contents of the array


First, the declaration of arrays in Perl

  • Perl uses the "@" symbol to declare an array: @array;

Second, the initialization of arrays in Perl

2.1. Directly assign initial values

$var = 2
@array = (12,"123","abc",$var);

2.2. Use qw to assign values ​​to the array

@array = qw(12 123 abc);

2.3. Arithmetic sequence

@array = (1..10)

Three, the basic method of the array

3.1. Access to array elements

@array=(1,2,3,4,5,6,7);

#取数组元素:

$array[0]        #表示数组的第一个元素 1,以此类推。
@array[1..3]     #表示数组的第二个元素到第四个元素:2,3,4
@array[1,2,4]    #表示数组的第二,第三和第五个元素:2,3,5;顺序也可以交换:@array[4,1,2]: 5 2 3;

#修改数组
#通过上述取数组元素的方法,取得元素,然后进行重新赋值。

$array[0]= -1;        # $array=(-1,2,3,4,5,6,7)
@array[1..2]=(8,9);   # $array=(1,8,9,4,5,6,7)

3.2. Traverse the array with foreach

#!user/bin/env perl -w
@array = qw(1 2 3 4 5 6);
$var = 100;

foreach $var (@array)
{
	print "$var\n";
}

print "var is : $var\n";

3.3. Append elements like an array

  • push(@array, $var)
  • unshift (@array, $ var); #Insert one or more elements at the beginning of the array and return the number of elements in the new array

3.4. Delete the elements in the array

  • pop(@array)
  • shift (); #Remove the first element from the array and remove the element
  • my @temp = grep {$ _ ne "apple"} @fruits; #Delete elements that are not equal to "apple"

3.5. Sorting of array elements

  • sort: Sort the characters bit by bit in the order in the ASCII code table
  • reverse: reverse the array
  • Examples:
my @scores = ( 10, 0, 4, 5, 2, 9, 8 );
#字符串数组升序:
my @temp = sort { $a cmp $b } @fruits;
#数字数组升序 :
my @temp = sort { $a <=> $b } @scores;
#字符串数组降序:
my @temp = sort { $b cmp $a } @fruits;
#数字数组升序 :
my @temp = sort { $b <=> $a } @scores;

3.6. Array empty

if(@list) {
 print "Not empty \n";
}

my $arr_num = scalar @arr1;       
if ($arr_num == 0) {
 print "empty \n";
}

 

Fourth, the special member variables of the array

4.1. $ # Array represents the index of the last element of the array, the length of the array is -1

4.2. @Array represents the length of the array, and printing directly will output the contents of the array

 

 

Published 211 original articles · praised 151 · 50,000+ views

Guess you like

Origin blog.csdn.net/gsjthxy/article/details/105517459
Recommended