Add values to the end of an array in Perl (push)

Home > Search
  by

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

@fruits = qw(apple banana orange grapes);

 

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

push @fruits, 'pineapple';

 

The array can be printed.

print @fruits;

 

Printing the array will show that "pineapple" has been appeneded to the end of the array.

apple banana orange grape pinnapple

 

Unshift can be used to append values to the beginning of an array.

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

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



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments