4.10.3. Discussion
The push function is optimized for appending a
list to the end of an array. You can take advantage of Perl's list
flattening to join two arrays, but this results in significantly more
copying than push:
@ARRAY1 = (@ARRAY1, @ARRAY2);
Here's an example of push in action:
@members = ("Time", "Flies");
@initiates = ("An", "Arrow");
push(@members, @initiates);
# @members is now ("Time", "Flies", "An", "Arrow")
To insert the elements of one array into the middle of another, use
the splice function:
splice(@members, 2, 0, "Like", @initiates);
print "@members\n";
splice(@members, 0, 1, "Fruit");
splice(@members, -2, 2, "A", "Banana");
print "@members\n";
This is the output:
Time Flies Like An Arrow
Fruit Flies Like A Banana