Updated July 3, 2023
Definition of Perl Unshift
The Perl unshift function is working the opposite of the shift function in the Perl technology for array operation. The Perlunshift function is useful for shifting the array elements in the right direction. The Perlunshift function is adding a new array element on the left side and old elements shifting on the right side. This function is useful for a list of array shifting and adding new elements on the left side of the array list.
Syntax:
The Perlunshift function syntax is below.
Perl unshiftsyntax :unshift(@old_array_list, 'new array elements');
The Perlunshift() method is used to shift the old array list toward the right direction and add a new one. The old array list variable is placed inside of Perl unshift method, and then-new array elements add to the old array variable. The new elements of the string array are placed inside of the single quotes (‘element of the array’) sign.
How does the Unshift Function Work in Perl?
- Download and install the Perl latest version on your operating system of the device.
https://www.Perl.org/ or http://strawberryPerl.com/ mostly using the Perl IDE website link. - Create a file with the .pl extension and save the file in the required command line path.
Example: helloo.pl or first pearl.pl - Create the array variable and initialize it with the required array list.
@ary_vrbl = ('learning', 'Perl', 'technology');
- Print the Initial value of the array list.
print "initial array list: @ary_vrbl \n";
- Use Perlunshift function for shifting the array list and add the required extra array elements.
unshift (@ary_vrbl, 'I', 'am') ;
- Print the array list after using Perlunshiftfunction.
print "Array list using perlUnshift function : @ary_vrbl";
- Combine the working procedure and run on the command line.
@ary_vrbl = ('learning', 'Perl', 'technology');
print "Initial array list: @ary_vrbl \n";
unshift(@ary_vrbl, 'I', 'am');
print "Array list using perlUnshiftfunction : @ary_vrbl"
Examples
Following are the examples are given below:
Example #1 – With string array list example and output.
@ary_vrbl = ('C', 'Java', 'Php', 'Perl');
print "Basic language To Know Coder: @ary_vrbl \n";
unshift(@ary_vrbl, 'Paython', 'DotNet', 'Kotlin', 'Scala');
print "Advance language TO know Coder: @ary_vrbl \n";
unshift(@ary_vrbl, 'Bootstrap', 'Jquery', 'HTML', 'CSS');
print "Web Technology TO know Coder: @ary_vrbl \n";
Output:
Description:
- The array list creates variables and initials with string values.
- The array returns the only initial value, the basic language to know the coder.
- The first Perl unshift function returns basic and advanced language to know coders in the array list.
- The second Perl unshift function returns basic and advanced language with web technology to know the coder.
Example #2 – With numerical array list example and output.
@ary_vrbl = (4, 5, 6, 7, 8, 9);
print "Basic value To Know user: @ary_vrbl \n";
unshift(@ary_vrbl, 1, 2, 3, 11);
print "Extra value TO know user: @ary_vrbl \n";
unshift(@ary_vrbl, 21, 32, 43, 54);
print "More value TO know user: @ary_vrbl \n";
Output:
Description:
- The array list creates variables and initials with numerical values.
- The array returns the only initial value, which is the basic value to know the user.
- The first Perl unshift function returns basic and extra values to know the user in the array list.
- The second Perl unshift function returns basic and extra values with more values to know the user.
Example #3 – With array list example and output.
@ary_vrbl = ( 14, 'Perl', 'Python', 7, 8);
print "Basic value To Know user: @ary_vrbl \n";
unshift(@ary_vrbl, 1,'Java', 34);
print "Extra value TO know user: @ary_vrbl \n";
unshift(@ary_vrbl, 'Perl', 27, 'Bootstrap');
print "More value TO know user: @ary_vrbl \n";
Output:
Description:
- The array list creates variables and initials with numerical and string values.
- The array returns basic values to know the user: numbers, Perl, and Python language.
- The first Perl unshift function returns basic and extra values to know the user in the array list: numbers and java.
- The second Perl unshift function returns more values to know the user, which are old and new array elements.
Example #4 – In array list with counting elements of array example and output.
@ary_vrbl = ('C', 'Java', 'Php', 'Perl');
print " Basic language To Know Coder: @ary_vrbl \n " ;
print "number of elements in the array list : ", unshift (@ary_vrbl, 'Paython', 'DotNet', 'Kotlin', 'Scala');
print "\n Advance language TO know Coder : @ary_vrbl \n " ;
print "number of elements in the array list : ", unshift (@ary_vrbl, 'Bootstrap', 'Jquery', 'HTML', 'CSS');
print "\n Web Technology TO know Coder: @ary_vrbl \n " ;
Output:
Description:
- The array list creates variables and initials with string values.
- The array returns the only initial value, the basic language to know the coder.
- The first Perl unshift function returns basic and advanced language to know the coder in the array list.
- The firstPerlunshift array has 8 elements in the array list.
- The second Perl unshift function returns basic and advanced language with web technology to know the coder.
- The secondPerl unshift array has twelve elements in the array list.
Example #5 – The basic function in the array list example and output.
@ary_vrbl = ('learning', 'Perl', 'technology');
print "Initial array list: @ary_vrbl \n " ;
print "Elements in the array list :", unshift(@ary_vrbl, 'I', 'am');
print "\n Array list using perlUnshiftfunction : @ary_vrbl " ;
Output:
Conclusion
- The Perlunshift function is used to modify the array list and sort the collection of strings.
- This function makes the array list user-friendly, organized, and adjustable.
- This function recreates the array list and adds new elements to the array.
Recommended Articles
This is a guide to Perl Unshift. Here we also discuss the definition and how the unshift function works in perl? along with examples. You may also have a look at the following articles to learn more –