Perl | unshift() Function
Last Updated :
06 Mar, 2023
unshift() function in Perl places the given list of elements at the beginning of an array. Thereby shifting all the values in the array by right. Multiple values can be unshift using this operation. This function returns the number of new elements in an array.
Syntax: unshift(Array, List)
Returns: Number of new elements in Array
Example 1:
Perl
#!/usr/bin/perl
@x = ( 'Geeks' , 'for' , 'Geeks' );
print "Original array: @x \n" ;
print "No of elements returned by unshift: " ,
unshift ( @x , 'Welcome' , 'to' );
print "\nUpdated array: @x" ;
|
Output:
Original array: Geeks for Geeks
No of elements returned by unshift: 5
Updated array: Welcome to Geeks for Geeks
Example 2:
Perl
#!/usr/bin/perl
@x = (10, 20, 30, 40, 50);
print "Original array: @x \n" ;
print "No of elements returned by unshift: " ,
unshift ( @x , 70, 80, 'Geeks' );
print "\nUpdated array: @x" ;
|
Output:
Original array: 10 20 30 40 50
No of elements returned by unshift: 8
Updated array: 70 80 Geeks 10 20 30 40 50
Example – 3 : Unshifting two lists.
Perl
#!/usr/bin/perl
my @arr_1 = ( "is" , "the" , "best" );
my @arr_2 = ( "Geeks" , "for" , "Geeks" );
unshift @arr_1 , @arr_2 ;
print ( "@arr_1" );
|
Output –
Share your thoughts in the comments
Please Login to comment...