Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





Shift and Unshift Functions in Perl


Tutorials Perl

Topic

What is Shift, Unshift Function?
How to add / remove an element from the beginning of an array?



Explanation
Add Elements to an Array:
Unshift Function:

Unshift is used to add an element to the begining of an array.

Syntax:
     unshift ARRAY, LIST;

In the above syntax the "ARRAY" specifies the array to which the elements in "LIST" are to be added in the begining.

Example:
    #! C:\programfiles\perl\bin\perl
     print "content-type: text/html\n\n";
     @color = ("red", "blue");
     unshift (@color, "green");
     $,="\n";
     print @color;
Result
    green red blue

The array @color will have three elements with "green" as the first element.

Remove Elements from an Array:
Shift Function:

Shift is used to remove an element from the begining of an array.

Syntax:
     shift (ARRAY);

In the above syntax "ARRAY" specifies the array from which an element need to be removed from the begining.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    @color = ("red", "blue", "green");
    shift (@color);
    $,="\n";
    print "@color";
Result:
     blue green

In the above example, the first element "red" is removed from the begining of an array, then the rest of the array is displayed.








A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting