Bootstrap FreeKB - Perl (Scripting) - Append element to the beginning of an array (unshift)
Perl (Scripting) - Append element to the beginning of an array (unshift)

Updated:   |  Perl (Scripting) articles

Let's say you have an array of fruit that contains apple, banana, orange, and grape.

@fruits = qw(apple banana orange grapes);

 

Unshift can be used to add values to the beginning of an array. In this example, pineapple is added to the beginning of the fruits array.

unshift @fruits, 'pineapple';

 

The array can be printed.

print @fruits;

 

Printing the array will show that "pinnapple" has been appended to the beginning of the array.

apple banana orange grape pineapple

 

Pop can be used to remove values from the end of an array.

Shift can be used to remove values to the beginning of an array.

Push can be used to append vaues to the end of an array.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 5d53fb in the box below so that we can be sure you are a human.