There are four common functions for array manipulation in Perl:
Push: Adds an element from the end of the array.
Pop: Extracting elements from the end of an array
Shift: Extracting elements from the beginning of an array
Unshift: Adding elements from the beginning of an array
1. Push
#!/usr/bin/perl
Use strict;
Use warnings;
My @array = ();
for (My $i = 1; $i <= 5; + + $i) {
Push @array, $i;
print "@array \ n";
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3) 4 5
2. Pop
#!/usr/bin/perl
####<pop>###
Use strict;
Use warnings;
My @array = (1, 2, 3, 4, 5, 6);
while (@array) {
My $firstTotal = Pop (@array);
print "@array \ n";
}
Output
1 2 3) 4 5
1 2 3 4
1 2 3
1 2
1
3, Shift
#!/usr/bin/perl
####<shift>###
Use strict;
Use warnings;
My @array = (1, 2, 3, 4, 5, 6);
while (@array) {
My $firstTotal = Shift (@array);
print "@array \ n";
}
Output
2 3 4) 5 6
3 4 5 6
4 5 6
5 6
6
4, Unshift
#!/usr/bin/perl
####<unshift>###
Use strict;
Use warnings;
My @array = ();
for (My $i = 1; $i <= 5; + + $i) {
Unshift (@array, $i); # Add $i to front of @array
print "@array \ n"; # Display Current @array
}
Output
1
2 1
3 2 1
4 3 2 1
5 4 3) 2 1
In addition, Perl arrays have other important functions, such as splice, SUBTR, split, join, sort, and so on:
5, splice The function of the middle part of the array operation:
5.1. Inserting content into the middle of the array
#!/usr/bin/perl
Use strict;
Use warnings;
My @array = (0.. 6);
My @array1 = (' a '). ' d ');
My @replaced = Splice (@array, 3, 2, @array1);
Print "Replaced: @replaced \ n",
"With: @array1 \ n",
"resulting in: @array \ n";
Output
Replaced:3 4
With:a b c D
Resulting in:0 1 2 a b C D 5 6
5.2. Delete array elements
#!/usr/bin/perl
Use strict;
Use warnings;
My @array = (0.. 6);
My @array1 = (' a '). ' d ');
My @replaced = Splice (@array, 3, 2);
Print "Replaced: @replaced \ n",
"resulting in: @array \ n";
Output
Replaced:3 4
With:a b c D
Resulting in:0 1 2 5 6
Delete to end
#!/usr/bin/perl
Use strict;
Use warnings;
My @array = (0.. 6);
My @array1 = (' a '). ' d ');
My @replaced = Splice (@array, 3);
Print "Replaced: @replaced \ n",
"resulting in: @array \ n";
Output
Replaced:3 4 5 6
Resulting in:0 1 2
6. Join each separate string in the connection list, generate a new string, return a scalar!
#!/usr/bin/perl
Use strict;
Use warnings;
My @array = (0.. 6);
My $replaced = join ("\ n", @array);
print "$replaced \ n",
Output
0
1
2
3
4
5
6
7. Split
Splits the string and puts the segmented result into the array
Perl-le ' $p =q (/var/ftp/test) @a=split (/\/ftp\//, $p);p rint $a [1]; '
Test
Perl-le ' $p =q (/var/ftp/test) @a=split (/\/ftp\//, $p);p rint $a [0]; '
/var
8. Scalar
The length of the statistical array, generally we do not use this, directly assign the array to scalar.
#!/usr/bin/perl
Use strict;
Use warnings;
My @array = (0.. 6);
My $count 1 = @array;
My $count 2 = scalar @array;
Print "$count 1\n";
Print "$count 2\n";
Output
7
7
9. Sort
Sorting an array element
#!/usr/bin/perl
Use strict;
Use warnings;
My @array = (0.. 9);
My @reversed = reverse @array;
Print "Original: @array \ n";
Print "Reversed: @reversed \ n";
# Create an unsorted array of numbers and sort it
My @array2 = (100, 23, 9, 75, 5, 10, 2, 50, 7, 96, 1, 40);
My @sortedLexically = sort @array2;
My @sortedNumerically = sort {$a <=> $b} @array2;
Print "unsorted: @array2 \ n";
Print "lexically: @sortedLexically \ n";
Print "numerically: @sortedNumerically \ n";
Output
Original:0 1 2 3 4 5 6 7 8 9
Reversed:9 8 7 6 5 4 3 2 1 0
unsorted:100 23 9 75 5 10 2 50 7 96 1 40
Lexically:1 10 100 2 23 40 5 50 7 75 9 96
Numerically:1 2 5 7 9 10 23 40 50 75 96 100
Perl Array Application detailed (push, pop, shift, Unshift)