Difference between revisions of "PERL/C2/Array-functions/English"
PoojaMoolya (Talk | contribs) |
|||
(One intermediate revision by one other user not shown) | |||
Line 8: | Line 8: | ||
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
− | | style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual | + | | style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center> |
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center> | | style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center> | ||
Line 53: | Line 53: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide | ||
− | | 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;"| Perl provides certain inbuilt '''functions'''. | + | | 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;"| '''Perl''' provides certain inbuilt '''functions'''. |
Line 65: | Line 65: | ||
This can be done by using; | This can be done by using; | ||
− | # '''push | + | # '''push function''' which adds an '''element''' at the last position of an '''Array''' |
− | # '''pop | + | # '''pop function''' which removes an '''element''' from the last position of an '''Array''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 75: | Line 73: | ||
− | | 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;"| Let us understand '''push''' and '''pop | + | | 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;"| Let us understand '''push''' and '''pop functions''' by using a sample program. |
Line 120: | Line 118: | ||
− | '''push | + | '''push function''' will insert an '''element''' at the last position of this '''Array''' i.e after 3<nowiki>;</nowiki> |
− | + | whereas, '''pop function '''will remove an '''element''' from the last position of the '''Array'''. | |
Line 137: | Line 135: | ||
− | | 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- | + | | 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 | + | # '''2nd argument '''is the''' element '''which is to be '''pushed''' into the '''Array.''' |
Line 151: | Line 149: | ||
* '''pop function''' takes only one''' argument''' | * '''pop function''' takes only one''' argument''' | ||
* It is the '''Array '''from which an '''element '''needs to be removed. | * It is the '''Array '''from which an '''element '''needs to be removed. | ||
− | |||
Line 176: | Line 173: | ||
− | + | | 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;"| Now switch to the '''terminal '''and execute the '''Perl script'''.''' ''' | |
− | | 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;"| Now switch to the '''terminal '''and | + | |
Line 199: | Line 195: | ||
'''New array after popping element''' | '''New array after popping element''' | ||
− | |||
− | |||
Line 211: | Line 205: | ||
# '''unshift function - '''which adds an''' element '''to an '''Array''' at the 1st position | # '''unshift function - '''which adds an''' element '''to an '''Array''' at the 1st position | ||
− | # '''shift function''' - which removes the | + | # '''shift function''' - which removes the first''' element '''from an '''Array.''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 243: | Line 235: | ||
− | '''unshift '''function will insert | + | '''unshift '''function will insert an '''element '''at the first position |
i.e before 1 | i.e before 1 | ||
− | '''shift '''function will remove | + | '''shift '''function will remove an '''element '''from the first position. |
− | In our case, | + | In our case, '''zero''' will be removed. |
|- | |- | ||
Line 266: | Line 258: | ||
# '''1st argument '''is the''' Array''' in which to add an '''element''' | # '''1st argument '''is the''' Array''' in which to add an '''element''' | ||
# '''2nd argument '''is the '''element '''to be added into the '''Array''' | # '''2nd argument '''is the '''element '''to be added into the '''Array''' | ||
− | |||
Line 274: | Line 265: | ||
* This is the '''Array '''from which the '''element '''needs to be removed. | * This is the '''Array '''from which the '''element '''needs to be removed. | ||
− | |||
Line 281: | Line 271: | ||
| 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 works at '''first position '''of an | + | Both these functions works at '''first position '''of an '''Array'''. |
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide''' | ||
− | | 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;"| We can collect the element removed by '''shift function''' into some variable. | + | | 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;"| We can collect the '''element''' removed by '''shift function''' into some variable. |
Line 303: | Line 291: | ||
− | | 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;"| Then switch to the '''terminal '''and | + | | 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;"| Then switch to the '''terminal '''and execute the '''Perl''' script. |
Line 326: | Line 314: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''slide''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''slide''' | ||
− | | 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;"| * '''splice function '''removes an '''element''' from a specified position of an '''Array'''. | + | | 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;"| |
+ | * '''splice function '''removes an '''element''' from a specified position of an '''Array'''. | ||
* The '''return value '''of this '''function '''is an '''Array '''of removed '''elements'''. | * The '''return value '''of this '''function '''is an '''Array '''of removed '''elements'''. | ||
− | |||
Line 336: | Line 324: | ||
− | Go to the '''perlArray dot pl''' file which we created earlier. | + | Go to the '''perlArray dot pl''' file, which we created earlier. |
|- | |- | ||
Line 348: | Line 336: | ||
'''print "Spliced Array: @splicedArray\n";''' | '''print "Spliced Array: @splicedArray\n";''' | ||
| 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;"| Type the piece of code shown on the screen. | | 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;"| Type the piece of code shown on the screen. | ||
− | |||
We need to provide | We need to provide | ||
Line 365: | Line 352: | ||
− | | 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;"| Then switch to the '''terminal '''and | + | | 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;"| Then switch to the '''terminal '''and execute the '''Perl script''' by typing - |
Line 386: | Line 373: | ||
* The return value of this '''function '''is an '''Array'''. | * The return value of this '''function '''is an '''Array'''. | ||
* The '''elements '''of this '''Array '''are the divided portions of the '''string'''. | * The '''elements '''of this '''Array '''are the divided portions of the '''string'''. | ||
− | |||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide | ||
− | | 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;"| * '''join function''' joins the '''elements '''of an '''Array, '''using the specified '''delimiter'''. | + | | 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;"| |
+ | * '''join function''' joins the '''elements '''of an '''Array, '''using the specified '''delimiter'''. | ||
* It returns a string of joined '''elements.''' | * It returns a string of joined '''elements.''' | ||
− | |||
Line 406: | Line 392: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;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;"| Now let us understand all these functions using a sample program. | + | | 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;"| Now let us understand all these '''functions''' using a sample program. |
|- | |- | ||
Line 417: | Line 403: | ||
'''gedit arrayFunctions dot pl space ampersand''' | '''gedit arrayFunctions dot pl space ampersand''' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 471: | Line 451: | ||
− | In this case, each word of the variable string will become an '''element '''of an '''Array.''' | + | In this case, each word of the '''variable string''' will become an '''element '''of an '''Array.''' |
Line 484: | Line 464: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide''' | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide''' | ||
− | | 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;"| Let us understand each function. | + | | 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;"| Let us understand each '''function'''. |
|- | |- | ||
Line 493: | Line 473: | ||
* '''2nd '''is the '''string '''which needs to be split. | * '''2nd '''is the '''string '''which needs to be split. | ||
− | '''Delimiters''' can be specified in | + | '''Delimiters''' can be specified in '''forward slash, single or double quotes.''' |
|- | |- | ||
Line 499: | Line 479: | ||
| 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;"| '''join function '''takes 2 '''arguments.''' | | 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;"| '''join function '''takes 2 '''arguments.''' | ||
− | + | * '''1st '''is the '''delimiter '''by which the '''Array elements '''needs to be joined'''.''' | |
− | + | * '''2nd '''is the '''Array.''' | |
− | '''Delimiters''' can be specified in '''single or double | + | '''Delimiters''' can be specified in '''single or double quotes.''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 521: | Line 499: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal | + | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''terminal'''and type |
− | + | ||
'''perl arrayFunctions.pl''' | '''perl arrayFunctions.pl''' | ||
− | + | | 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;"| Then switch to the '''terminal '''and execute the '''Perl script''' by typing - | |
− | + | ||
− | | 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;"| Then switch to the '''terminal '''and | + | |
Line 576: | Line 551: | ||
'''];''' | '''];''' | ||
| 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;"| The following output is displayed on the '''terminal'''<nowiki>. <pause></nowiki> | | 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;"| The following output is displayed on the '''terminal'''<nowiki>. <pause></nowiki> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 592: | Line 562: | ||
* basic functions which can be performed on '''Array''' | * basic functions which can be performed on '''Array''' | ||
* using sample programs | * using sample programs | ||
− | |||
Line 599: | Line 568: | ||
| 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;"| Here is assignment for you - | | 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;"| Here is assignment for you - | ||
− | # ' | + | # 'script.spoken-tutorial.org/index.php/Perl script.spoken-tutorial.org/index.php/Perl]' |
+ | # split the above '''string''' at '/' '''delimiter''' | ||
# Add '''https:// '''at the start of an newly created '''Array''' | # Add '''https:// '''at the start of an newly created '''Array''' | ||
− | # Remove '''element “Perl” '''from | + | # Remove '''element “Perl” '''from the '''Array''' |
# Declare number '''Array '''and sort it | # Declare number '''Array '''and sort it | ||
− | |||
− | |||
|- | |- |
Latest revision as of 13:01, 17 June 2014
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 |
|
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.
We need to provide
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 |
|
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 terminaland type
perl arrayFunctions.pl |
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.
|