Remove values from the beginning of an array in Perl (shift)

Home > Search
  by

Let's take for example the following array.

@fruit = qw(banana apple orange grapes);

 

Printing the array will print all of the values in the array.

banana apple orange grapes

 

Shift can be used to remove values from the beginning of the array. In this example, "banana" is removed from the @fruit array.

shift @fruit;

 

Printing the array will show that "banana" has been removed from the array.

apple orange grapes

 

If needed, you can store the items being removed from the array in a variable.

$banana = shift @fruit;

 

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

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

Push can be used to append vaues to the end 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