The shift()
method in Perl is used to return the first value of an array. It removes the first element and shifts the array list elements to the left by one.
shift(@arr)
@arr
: This is the array whose elements we want to shift.
It returns the first element of the array after removing it from the array. However, if the array is empty, it returns -1
.
# create an array@arr = ("e", "a", "b", "c", "d");# print array valuesprint "Before shift: @arr \n";# shift element@shifted_element = shift(@arr);# print shifted elementprint "Shifted Element: @shifted_element";# print array valuesprint "\nAfter shift: @arr";
arr
.arr
to the console.shift()
method.