Difference between revisions of "PERL/C2/Arrays/English"
Nancyvarkey (Talk | contribs) |
Nancyvarkey (Talk | contribs) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 17: | Line 17: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives | ||
− | | 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;"| In this tutorial, we will learn about | + | | 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;"| In this tutorial, we will learn about |
* '''Index''' of an '''array''' | * '''Index''' of an '''array''' | ||
Line 29: | Line 29: | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements | ||
| 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 I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2''' | | 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 I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2''' | ||
+ | |||
I will also be using the '''gedit '''Text Editor. | I will also be using the '''gedit '''Text Editor. | ||
Line 37: | Line 38: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites | ||
− | | 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;"| You should have basic knowledge of '''Variables & Data Structures '''in''' Perl''' | + | | 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;"| You should have basic knowledge of '''Variables, Comments & Data Structures '''in''' Perl''' |
− | Knowledge of''' | + | Knowledge of''' loops and conditional statements''' will be an added advantage'''.''' |
Line 47: | Line 48: | ||
|- | |- | ||
| 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;"| * '''Array''' is a simple '''data structure''' which contains '''elements''' of any '''data type'''. | + | | 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;"| |
+ | * '''Array''' is a simple '''data structure''' which contains '''elements''' of any '''data type'''. | ||
* '''Array index''' starts from zero always. | * '''Array index''' starts from zero always. | ||
* In '''Perl''', it is not necessary to declare the length of an '''array'''. | * In '''Perl''', it is not necessary to declare the length of an '''array'''. | ||
Line 55: | Line 57: | ||
|- | |- | ||
| 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;"| The syntax for declaring an array is - | + | | 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 syntax for declaring an '''array''' is - |
'''@myArray = (1, 2, 3, 'abc', 10.3);''' | '''@myArray = (1, 2, 3, 'abc', 10.3);''' | ||
Line 67: | Line 69: | ||
|- | |- | ||
| 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;"| Let us understand this using | + | | 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 this using sample '''program'''. |
|- | |- | ||
Line 91: | Line 93: | ||
'''@myArray = (1, 2, 3, 'abc', 10.3);''' | '''@myArray = (1, 2, 3, 'abc', 10.3);''' | ||
− | '''print “Last index of myArray is: | + | '''print “Last index of myArray is: |
− | + | $#myArray\n”;''' | |
Line 99: | Line 101: | ||
<nowiki><<pause>></nowiki> | <nowiki><<pause>></nowiki> | ||
+ | |||
+ | |||
Line 112: | Line 116: | ||
− | i.e number of elements, which is 5, minus 1. | + | i.e number of '''elements''', which is 5, minus 1. |
|- | |- | ||
Line 119: | Line 123: | ||
|- | |- | ||
− | | 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 execute the '''Perl''' script. |
Type | Type | ||
− | '''perl arrayIndex.pl''' | + | '''perl arrayIndex.pl''' and press '''Enter'''. |
− | | 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;"| Type -''' ''' |
Line 140: | Line 144: | ||
Last index of myArray is: 4 | Last index of myArray is: 4 | ||
− | | 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;"|The output will be as displayed on the terminal |
− | + | ||
'''Last index of myArray is: 4''' | '''Last index of myArray is: 4''' | ||
+ | |||
|- | |- | ||
| 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 | + | | 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 see how to get length of an '''array '''in '''Perl'''. |
|- | |- | ||
| 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;"| There are many ways by which we can find the length 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;"| There are many ways by which we can find the length of an '''array. ''' | ||
− | |||
− | |||
− | |||
# Index of an '''array''' + 1; '''$#array + 1''' | # Index of an '''array''' + 1; '''$#array + 1''' | ||
Line 176: | Line 177: | ||
Press '''Enter.''' | Press '''Enter.''' | ||
− | |||
− | |||
|- | |- | ||
Line 191: | Line 190: | ||
− | '''print "Length of an array using index: ", ''' | + | '''print "Length of an array using index: ", $#myArray + 1, "\n"; ''' |
− | |||
+ | '''print "Length of an array using | ||
− | + | scalar variable: $length\n"; ''' | |
− | |||
+ | '''print "Length of an array using | ||
− | + | scalar function: ", scalar(@myArray), "\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 following piece of code, as 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 following piece of code, as shown on the screen- | ||
− | |||
− | |||
Here we have declared & defined an '''array''' which contains '''5 elements'''. | Here we have declared & defined an '''array''' which contains '''5 elements'''. | ||
Line 215: | Line 210: | ||
− | Highlighted | + | Highlighted, are various ways to find the length of an '''array '''in '''Perl.''' |
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight comma in print statement | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight comma in print statement | ||
− | | 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;"| '''Please 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;"| '''Please note -''' |
− | We have '''concatenated''' the output in the '''print | + | We have '''concatenated''' the output in the '''print statement''' using '''comma.''' |
|- | |- | ||
Line 230: | Line 225: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal | ||
+ | |||
Line 245: | Line 241: | ||
Length of an array using scalar function: 5 | Length of an array using scalar function: 5 | ||
− | | 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 output | + | | 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 output will be as displayed on the '''terminal.''' |
+ | |||
|- | |- | ||
| 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;"| Next, let us understand | + | | 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;"| Next, let us understand how to access individual '''elements '''in an '''array'''. |
|- | |- | ||
Line 258: | Line 255: | ||
|- | |- | ||
| 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 | ||
+ | |||
Line 275: | Line 273: | ||
and press '''Enter.''' | and press '''Enter.''' | ||
− | |||
− | |||
− | |||
Line 292: | Line 287: | ||
'''<nowiki># Access the first element of an array </nowiki>''' | '''<nowiki># Access the first element of an array </nowiki>''' | ||
− | '''<nowiki>print "First element of myArray is: ", | + | '''<nowiki>print "First element of myArray is: ", $myArray[0], "\n"; </nowiki>''' |
− | + | ||
− | + | ||
'''<nowiki># Access 3rd element of an array </nowiki>''' | '''<nowiki># Access 3rd element of an array </nowiki>''' | ||
− | '''<nowiki>print "3rd Element of myArray is: ", | + | '''<nowiki>print "3rd Element of myArray is: ", $myArray[2], "\n"; </nowiki>''' |
− | + | ||
− | + | ||
'''<nowiki># Access last element of an array </nowiki>''' | '''<nowiki># Access last element of an array </nowiki>''' | ||
− | '''<nowiki>print "Last element of myArray is: ", ''' | + | '''<nowiki>print "Last element of myArray is: ", $myArray[$#myArray], "\n";</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;"| Type the following piece of code as shown. | | 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 following piece of code as shown. | ||
− | |||
− | |||
|- | |- | ||
Line 323: | Line 311: | ||
− | '''<nowiki>print "First element of myArray is: ", </nowiki>''' | + | '''<nowiki>print "First element of myArray is: ", $myArray[0], "\n"; </nowiki>''' |
− | |||
− | + | '''<nowiki>print "Last element of myArray is: ", $myArray[$#myArray], "\n";</nowiki>''' | |
− | '''<nowiki>print "Last element of myArray is: ", | + | | 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;"| Please 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;"| Please note- | + | |
# '''myArray''' is declared with '''@ (at the rate)''' sign. | # '''myArray''' is declared with '''@ (at the rate)''' sign. | ||
# But, to access an '''array element '''we need to use '''$ (dollar) '''sign. | # But, to access an '''array element '''we need to use '''$ (dollar) '''sign. | ||
− | # To access the '''element '''at | + | # To access the '''element '''at any position, we need to pass '''index ''' to an array |
− | + | ||
− | + | ||
− | + | ||
− | Here, to access the first''' element '''of '''myArray ''' | + | Here, to access the first''' element '''of '''myArray ''', |
zero is passed as '''index.''' | zero is passed as '''index.''' | ||
− | To access the last element of '''myArray '''we have passed the last '''index '''to '''myArray.''' | + | To access the last element of '''myArray ''', we have passed the last '''index '''to '''myArray.''' |
Recall, we had learnt about this earlier. | Recall, we had learnt about this earlier. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press ctrl + s | ||
+ | | 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;"| Press '''Ctrl + S''' to save the file. | ||
+ | |||
+ | |- | ||
+ | | 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 | ||
+ | |||
+ | |||
+ | '''perl perlArray.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''' as''' -''' | ||
+ | |||
+ | |||
+ | '''perl perlArray dot pl''' | ||
+ | |||
+ | |||
+ | and press''' Enter.''' | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal | ||
+ | | 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 output will be as shown on the terminal | ||
|- | |- | ||
Line 360: | Line 362: | ||
# Using '''for loop''' | # Using '''for loop''' | ||
# Using '''foreach loop''' | # Using '''foreach loop''' | ||
− | |||
|- | |- | ||
Line 391: | Line 392: | ||
'''<nowiki># Do the operation on individual element </nowiki>''' | '''<nowiki># Do the operation on individual element </nowiki>''' | ||
− | '''<nowiki>print "Printing element using for loop: ", | + | '''<nowiki>print "Printing element using for loop: ", $myArray[$i], "\n"; </nowiki>''' |
− | + | ||
− | + | ||
'''} ''' | '''} ''' | ||
Line 400: | Line 399: | ||
'''<nowiki># Used to separate the output on terminal </nowiki>''' | '''<nowiki># Used to separate the output on terminal </nowiki>''' | ||
− | '''print " | + | '''print "====================\n"; ''' |
− | + | ||
− | + | ||
Line 411: | Line 408: | ||
'''<nowiki># Do the operation on individual element </nowiki>''' | '''<nowiki># Do the operation on individual element </nowiki>''' | ||
− | '''print "Printing element using foreach loop: | + | '''print "Printing element using foreach loop: $element\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;"| | + | | 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 as shown on the screen |
− | + | Here, we are printing each '''element''' of the '''array''' by iterating the '''index'''. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | Here, we are printing each '''element''' of the '''array''' | + | |
Line 441: | Line 431: | ||
* If you are not aware of '''for''' and''' foreach loops''', | * If you are not aware of '''for''' and''' foreach loops''', | ||
* please go through the relevant tutorials | * please go through the relevant tutorials | ||
− | * on the spoken tutorial website. | + | * on the '''spoken tutorial''' website. |
− | + | ||
|- | |- | ||
Line 452: | Line 441: | ||
− | '''perl | + | '''perl loopingOverArray.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''' as''' -''' | | 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''' as''' -''' | ||
Line 473: | Line 460: | ||
'''Printing element using for loop: 3 ''' | '''Printing element using for loop: 3 ''' | ||
− | '''<nowiki> | + | '''<nowiki>====================</nowiki>''' |
'''Printing element using foreach loop: 1 ''' | '''Printing element using foreach loop: 1 ''' | ||
Line 480: | Line 467: | ||
'''Printing element using foreach loop: 3 ''' | '''Printing element using foreach loop: 3 ''' | ||
− | | 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 | + | | 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 output will be as displayed on the '''terminal'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 492: | Line 475: | ||
* '''@alphaArray = (a..d); ''' | * '''@alphaArray = (a..d); ''' | ||
* i.e '''alphaArray''' will contain '''elements''' 'a', 'b', 'c' and 'd' | * i.e '''alphaArray''' will contain '''elements''' 'a', 'b', 'c' and 'd' | ||
− | * Similarly | + | * Similarly, '''@numericArray = (1..5);''' is same as '''@numericArray = (1, 2, ,3, 4, 5);''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 504: | Line 485: | ||
− | '''@ | + | '''@array = (19, 23, 56, 45, 87, 89); ''' |
− | '''<nowiki>@ | + | '''<nowiki>@newArray = @myarray[1, 4];</nowiki>''' |
− | After '''slicing''', ''' | + | After '''slicing''', '''newArray''' will look like this - |
− | '''@ | + | '''@newArray = (23, 87);''' |
|- | |- | ||
Line 537: | Line 518: | ||
# Print '''Length '''and last '''index '''of this '''array''' | # Print '''Length '''and last '''index '''of this '''array''' | ||
# '''Loop over''' each '''element''' of the '''array''' using '''for''' & '''foreach loops''' | # '''Loop over''' each '''element''' of the '''array''' using '''for''' & '''foreach loops''' | ||
+ | # Declare '''array''' as @myArray = (1..9); and then create an '''array''' of odd numbers from above '''array''' using '''array slicing'''. | ||
+ | |||
+ | |||
|- | |- |
Latest revision as of 17:31, 12 November 2013
Title Of Script: Arrays in Perl
Author: Amol Brahmankar
Keywords: Array in perl video tutorial.
|
|
Slide | Welcome to the spoken tutorial on Arrays in Perl. |
Slide: Learning Objectives | In this tutorial, we will learn about
|
Slide: System Requirements | Here I am using Ubuntu Linux12.04 operating system and Perl 5.14.2
|
Slide: Prerequisites | You should have basic knowledge of Variables, Comments & Data Structures in Perl
|
Slide |
|
Slide | The syntax for declaring an array is -
@myArray = (1, 2, 3, 'abc', 10.3); |
Slide | The last index of an array can be found with this command -
$#myArray |
Let us understand this using sample program. | |
Terminal
gedit arrayIndex.pl & >> Enter |
Open the terminal and type
gedit arrayIndex dot pl space ampersand and press Enter.
|
gedit
print “Last index of myArray is: $#myArray\n”;
|
Type the piece of code that is displayed on screen
<<pause>>
|
@myArray = (1, 2, 3, 'abc', 10.3);
|
Here we have declared & defined an array which contains 5 elements.
|
Press Ctrl+S to save the file. | |
Switch to terminal and execute the Perl script.
perl arrayIndex.pl and press Enter.
|
Type -
|
Highlight the output on the terminal
|
The output will be as displayed on the terminal
Last index of myArray is: 4
|
Now, let us see how to get length of an array in Perl. | |
Slide | There are many ways by which we can find the length of an array.
|
Terminal
|
Let us look at an illustration of array length using a sample program.
gedit arrayLength dot pl space ampersand
|
Gedit
$length = @myArray;
scalar variable: $length\n";
scalar function: ", scalar(@myArray), "\n"; |
Type the following piece of code, as shown on the screen-
|
Highlight comma in print statement | Please note -
We have concatenated the output in the print statement using comma. |
Press Ctrl + S to save the file. | |
Terminal
|
Now let us execute the script.
Switch to terminal and type-
|
Length of an array using index: 5
Length of an array using scalar variable: 5 Length of an array using scalar function: 5 |
The output will be as displayed on the terminal.
|
Next, let us understand how to access individual elements in an array. | |
Slide | Indexing is used to access elements of an array. |
Slide
|
Let us look at an example for accessing elements of an array at -
|
Terminal | Switch to the terminal and type -
and press Enter.
|
Gedit
print "First element of myArray is: ", $myArray[0], "\n";
print "3rd Element of myArray is: ", $myArray[2], "\n";
print "Last element of myArray is: ", $myArray[$#myArray], "\n";
|
Type the following piece of code as shown.
|
Highlight
|
Please note-
Here, to access the first element of myArray , zero is passed as index.
|
Press ctrl + s | Press Ctrl + S to save the file. |
Switch to terminal
|
Then switch to the terminal and execute the Perl script as -
|
Terminal | The output will be as shown on the terminal |
Now, let us understand, how to loop over each element of an array. | |
Slide | There are two ways of looping over an array-
|
Let's learn how to use these loops to iterate over an array using a sample program. | |
Terminal | For this, switch to the terminal and type
gedit loopingOverArray dot pl space ampersand
|
Gedit
for ($i = 0; $i <= $#myArray; $i++) { # Do the operation on individual element print "Printing element using for loop: ", $myArray[$i], "\n"; }
print "====================\n";
foreach $element (@myArray) { # Do the operation on individual element print "Printing element using foreach loop: $element\n"; } |
Type the piece of code as shown on the screen
|
Slide | Please Note:
|
Press ctrl + s | Press Ctrl + S to save the file. |
Switch to terminal
|
Then switch to the terminal and execute the Perl script as -
|
Highlight the output on the terminal
Printing element using for loop: 2 Printing element using for loop: 3 ==================== Printing element using foreach loop: 1 Printing element using foreach loop: 2 Printing element using foreach loop: 3 |
The output will be as displayed on the terminal. |
Slide | In Perl, we can declare a sequential array as-
|
Slide | Perl also provides array slicing.
@newArray = @myarray[1, 4];
@newArray = (23, 87); |
Slide: Summary | Let us summarize.
using sample programs. |
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.
|