Perl Array and hash correlation function

The built-in array functions are :
each, keys, pop, push, shift, splice, unshift, values
Built in hash The function has :
delete, each, exists, keys, values
Some are repetitive . So put them together to explain .

Array correlation function :

* push: Appends a single element or list to the end of an array , Returns an example of the length of the appended array push
<https://www.cnblogs.com/f-ck-need-u/p/9680347.html#blogpush>

* pop: Delete the last element in the array , Returned by pop Lost elements , Specific examples top
<https://www.cnblogs.com/f-ck-need-u/p/9680347.html#blogpop>

* unshift: Appends a single element or list to the head of an array , Returns an example of the length of the appended array push
<https://www.cnblogs.com/f-ck-need-u/p/9680347.html#blogpush>

* shift: Delete the first element in the array , Returned by shift Lost elements , Specific examples top
<https://www.cnblogs.com/f-ck-need-u/p/9680347.html#blogpop>

* splice: Delete from specified location , Insert element , Specific examples splice
<https://www.cnblogs.com/f-ck-need-u/p/9680347.html#blogsplice>
hash correlation function :( It's relatively simple , So I don't introduce it )

* delete: Delete given key

* exists: test key Does it exist in hash in
Common function :( It's relatively simple , So I don't introduce it )

* keys: obtain hash Or array (5.12 Version is only available ) Of key, For arrays , It returns the index position . about hash, Returned key The order of is uncertain

* values: return hash Or array (5.12 Version is only available ) Value of .values The returned element order and keys The order of return is consistent

* each: ergodic hash Or array
<>

push & unshift

Appends a list to the end of an array , Returns the length of the appended array .
push ARRAY,LIST unshift ARRAY,LIST
* push Is to make a list ( There may be only one element ) Append to the end of the array

* unshift A list will be created ( There may be only one element ) Append to the front of the array

* They return the length of the appended array

* When appending the list , Not one element at a time , It's appending an entire list at a time , So the order of the new elements appended to the array remains the same
be careful ,unshift Appends a list to the beginning of the array , This will cause the original element index in the array to move backward as a whole . For example, the contents of the original array are a b c d
,unshift After a new element ,a It becomes the second element , Index position from 0 become 1,b Index position of from 1 become 2,c Index position of from 2 become 3,d Index position of from 3 become 4. When the array is small ,unshift It won't make any difference , But the array is large , It will seriously affect efficiency .

for example :
@arr=qw(python shell php); push @arr,"perl","Ruby"; print "@arr"; # output :python
shell php perl Ruby
push The return value of is after successfully appending the element , The length of the array :
@arr=qw(python shell php); print push @arr,"perl","Ruby"; # output :5
pop operation , Equivalent in result to :
for my $value (@LIST){ $ARRAY[++$#ARRAY] = $value; }
but push More efficient , Because it appends an entire list at a time , Instead of appending elements one at a time .

<>

pop & shift
pop ARRAY pop shift ARRAY shift
* pop Removes and returns the last element from the array
* shift Removes and returns the first element from an array

* If the array is empty , be pop/shift return undef

* If omitted ARRAY, be pop/shift The operation is @ARGV, But if it's in a subroutine , Then the operation is @_
be careful ,shift Deleting the first element causes the index position of the array to move forward as a whole . For example, the contents of the array are a b c d
, but shift After one time ,b It becomes the first element , Its index position is from 1 become 0,c Index position of from 2 become 1,d Index position of from 3 become 2. When the array is small ,shift It won't make any difference , But the array is large , It will seriously affect efficiency .

for example : Loop through the last element of the array
@arr=qw(python shell php); while(my $poped = pop @arr){ print $poped,"\n"; #
Output in sequence :php shell python }
When omitted ARRAY Time , And not in the subroutine ,pop Will operate @ARGV.
while(my $poped=pop){ print $poped,"\n"; }
Carry it out :
$ ./test.pl a.txt b.txt c.txt c.txt b.txt a.txt
In the same way , In subroutines , The default parameter array is operated @_.
sub mysub() { while(my $poped=pop){ print $poped,"\n"; } } &mysub(qw(a.txt
b.txt c.txt)); # Will output in turn c.txt b.txt a.txt
<>

splice

pop/push,unshift/shift All operations are at the beginning of the array , Or at the end .splice( Chinese Translation : Bonding ) You can specify which position in the operation array .
splice ARRAY splice ARRAY,OFFSET splice ARRAY,OFFSET,LENGTH splice
ARRAY,OFFSET,LENGTH,LIST
* splice When removing an element , Returns the list of removed elements in the list context , Scalar context returns the last removed element
1. One parameter , Namely splice ARRAY, It means empty ARRAY.
use 5.010; @arr=qw(perl py php shell); @new_arr=splice @arr; say "original
arr: @arr"; # output : empty say "new arr: @new_arr"; # Output the contents of the original list
If splice In scalar context , The last removed element is returned :
use 5.010; @arr=qw(perl py php shell); $new_arr=splice @arr; say "$new_arr"; #
output :shell
2. Two parameters , Namely splice ARRAY,OFFSET, From OFFSET Delete the element until the end .

be careful ,OFFSET It can be negative .

for example :
use 5.010; @arr=qw(perl py php shell); @new_arr=splice @arr,2; say "original
arr: @arr"; # output :perl py say "new arr: @new_arr"; # output :php shell
If offset Is negative , Is the number of elements from back to front ,-1 Represents the last element .
use 5.010; @arr=qw(perl py php shell); @new_arr=splice @arr,-3; say "original
arr: @arr"; # output :perl say "new arr: @new_arr"; # output :py php shell
3. Three parameters , Namely splice ARRAY,OFFSET,LENGTH, From OFFSET Start deleting backward at LENGTH Elements .

be careful ,LENGTH Can be negative , You can also 0, They all work wonders .

for example :
use 5.010; @arr=qw(perl py php shell ruby); @new_arr=splice @arr,2,2; say
"original arr: @arr"; # output :perl py ruby say "new arr: @new_arr"; # output :php shell
If length Is negative ( Suppose it is -3), From offset Start deleting at , Until the tail remains -length Elements (-3 Time , That is, the tail is reserved 3 Elements ). for example :
use 5.010; @arr=qw(perl py php shell ruby java c c++ js); @new_arr=splice
@arr,2,-2; # from php Start deleting , In the end, only keep it c++ and js Two elements say "original arr: @arr"; # output :perl py
c++ js say "new arr: @new_arr"; # output :php shell ruby java c

If the number is positive length The length of is beyond the bounds of the array , Delete the end . If it's negative length Out of bounds , In other words, there are more reserved than deleted , The retention priority is higher , That is, it will not be deleted . for example , Delete from a location , There's more on the back 2 Elements , But if "length=-2", The two elements are not deleted .

If length by 0, Is not deleted , This is in the beginning 4 Parameters LIST It's useful .

4. Four parameters , Namely splice ARRAY,OFFSET,LENGTH,LIST, Means will LIST Insert to deleted location , That is to replace the elements of the array that are in continuous positions .

for example :
use 5.010; @arr=qw(perl py php shell ruby); @list=qw(java c); @new_arr=splice
@arr,2,2,@list; say "original arr: @arr"; # output :perl py java c ruby say "new
arr: @new_arr"; # output :php shell
If you want to insert new elements in place , Without deleting any elements , You can length Set to 0, It inserts a new list into the offset Location of .
use 5.010; @arr=qw(perl py php shell ruby); @list=qw(java c); @new_arr=splice
@arr,2,0,@list; say "original arr: @arr"; # output :perl py java c php shell ruby
say "new arr: @new_arr"; # output : empty
Pay attention to the top php After the newly inserted element .


splice The function is very easy to use , In many languages , To achieve a similar function , It needs to be realized by means of linked list , It's complicated .splice One function is enough , It's much more convenient , But the linked list has increased , The efficiency of deleting elements is high , Therefore, for large arrays, we need to add, delete and modify them frequently , Or is the ready-made list better .