Difference between revisions of "PERL/C2/Array-functions/English"
(Created page with ''''Title Of Script: '''Array functions in Perl '''Author:''' Amol Brahmankar '''Keywords:''' Array functions in perl video tutorial. {| style="border-spacing:0;" | style="bo…') |
|||
Line 65: | Line 65: | ||
This can be done by using; | This can be done by using; | ||
− | |||
# '''push''' '''function''' which adds an '''element''' at the last position of an '''Array''' | # '''push''' '''function''' which adds an '''element''' at the last position of an '''Array''' | ||
+ | # '''pop''' '''function''' which removes an '''element''' from the last position of an '''Array''' | ||
Line 137: | Line 137: | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''push function''' takes 2 arguments- |
− | # '''1st argument '''to the '''push function '''is the '''Array''' in which to add an''' element.''' | + | # '''1st argument '''to the '''push function, '''is the '''Array''' in which to add an''' element.''' |
# '''2nd argument '''is the''' element '''which is''' '''to be '''pushed''' into the '''Array.''' | # '''2nd argument '''is the''' element '''which is''' '''to be '''pushed''' into the '''Array.''' | ||
Line 150: | Line 150: | ||
* '''pop function''' takes only one''' argument''' | * '''pop function''' takes only one''' argument''' | ||
− | * It is the ''' | + | * It is the '''Array '''from which an '''element '''needs to be removed. |
Line 158: | Line 158: | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Note:''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Note:''' | ||
− | Both these functions | + | Both these functions work at '''last position''' of an '''Array.''' |
|- | |- |
Revision as of 12:41, 29 August 2013
Title Of Script: Array functions in Perl
Author: Amol Brahmankar
Keywords: Array functions in perl video tutorial.
|
|
Slide | Welcome to the spoken tutorial on Array Functions in Perl |
Slide: Learning Objectives | In this tutorial, we will learn about Array functions in Perl, like
|
Slide: System Requirements | I am using Ubuntu Linux12.04 operating system and Perl 5.14.2
|
Slide: Prerequisites | You should have basic knowledge of Variables, Data Structures & Arrays in Perl
|
Slide | Perl provides certain inbuilt functions.
|
Slide | Let us first learn how to add and remove elements from last position of an Array.
|
Terminal
|
Let us understand push and pop functions by using a sample program.
|
Gedit
push (@myArray, 4);
pop (@myArray); print "New array after popping element: @myArray\n"; |
This will open perlArray dot pl file in gedit.
<<pause>>
|
Press Ctrl + S to save the file. | |
Slide
|
push function takes 2 arguments-
|
Slide | The syntax of pop function is as follows -
|
Slide | Note:
Both these functions work at last position of an Array. |
Slide | The element removed by pop function can be collected into another variable.
$variable = pop(@myArray) |
Switch to terminal
|
Now switch to the terminal and execute the Perl script.
|
Highlight the output on the terminal
New array after pushing 4 into an array: 1 2 3 4 New array after popping element: 1 2 3 |
The output is shown on the terminal.
New array after pushing 4 into an array New array after popping element
|
Slide | Now, let us see how to add/remove an element from the 1st position of an Array.
|
Gedit | Let us understand this using a sample program.
|
Gedit
unshift (@myArray, 0);
shift (@myArray);
|
Type the following piece of code as shown on the screen.
i.e before 1
|
Press Ctrl + S to save the file. | |
Slide
|
The unshift function takes 2 arguments -
|
Slide | shift function takes only one argument -
|
Slide | Note:
Both these functions works at first position of an array.
|
Slide | We can collect the element removed by shift function into some variable.
$variable = shift(@myArray) |
Switch to terminal
|
Then switch to the terminal and execute the Perl script.
|
Highlight the output on the terminal
Array after removing element from 1st position: 1 2 3
|
The output displayed on the terminal is as highlighted. |
Now, let us see how to remove an element from a specified position of an Array. | |
slide | * splice function removes an element from a specified position of an Array.
|
Gedit | Let us understand this using a sample program.
|
# Removes element(s) from the specified position
@myNewArray = (1, 2, 3, 4, 5, 6, 7, 8, 9); @splicedArray = splice(@myNewArray, 4, 2);
|
Type the piece of code shown on the screen.
In our case, the elements 5 and 6 will be removed. |
Switch to terminal
|
Then switch to the terminal and execute the Perl script by typing -
|
Highlight the output on the terminal
|
The output displayed on the terminal is as highlighted. |
Now let us look at few other inbuilt functions of Arrays. | |
Slide | * split function is used to divide a string at a specified delimiter.
|
Slide | * join function joins the elements of an Array, using the specified delimiter.
|
Slide | sort function sorts an Array in alphabetical/numerical order. |
Slide | qw function returns an Array of words, separated by a white space. |
Now let us understand all these functions using a sample program. | |
Terminal
|
Switch to terminal and type
|
Gedit
use Data::Dumper;
@newArray = split (/ /, $string);
print "Array after split: \n"; print Dumper \@newArray; print "\n";
print "String After Join: $joinedString\n\n";
print "Array after sorting: @sortedArray\n\n";
print "Array after qw: \n"; print Dumper \@qwArray;
|
Type the following piece of code as shown.
|
Slide | Let us understand each function. |
Slide | split function takes two arguments.
Delimiters can be specified in forward slash, single or double quotes. |
Slide | join function takes 2 arguments.
Delimiters can be specified in single or double quotes. |
Slide | sort function takes a single argument, which is the Array that needs to be sorted. |
Slide | qw function returns an Array of words, separated by space.
It is not necessary to specify the word in quotes, if written using qw. |
Press ctrl + s | Press Ctrl + S to save the file. |
Switch to terminal
|
Then switch to the terminal and execute the Perl script by typing -
|
Highlight the output on the terminal
$VAR1 = [ 'Illustration', 'Of', 'Array', 'Functions' ];
$VAR1 = [ 'Hello', 'Perl', 'Array' ]; |
The following output is displayed on the terminal. <pause>
|
Slide: Summary | Let us summarize.
In this tutorial, we have learnt to -
|
Slide: Assignment | Here is assignment for you -
|
About the Project | Watch the video available at the following link
download and watch it |
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
test
contact at spoken hyphen tutorial dot org |
Acknowledgment | Spoken Tutorial Project is a part of the Talk to a
Teacher project
Education through ICT, MHRD, Government of India.
spoken hypen tutorial dot org slash NMEICT hyphen Intro |
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|