Difference between revisions of "PERL/C2/Arrays/English"
(Created page with ''''Title Of Script: Arrays in Perl''' '''Author:''' Amol Brahmankar '''Keywords: '''Array in perl video tutorial. {| style="border-spacing:0;" | style="border-top:0.05pt sol…') |
Nancyvarkey (Talk | contribs) |
||
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 95: | Line 95: | ||
'''@myArray = (1, 2, 3, 'abc', 10.3);''' | '''@myArray = (1, 2, 3, 'abc', 10.3);''' | ||
− | '''print “Last index of myArray is: $#myArray\n”;''' | + | '''print “Last index of myArray is: ''' |
+ | |||
+ | '''$#myArray\n”;''' | ||
Line 201: | Line 203: | ||
− | '''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: | ||
− | '''print "Length of an array using scalar function: ", scalar(@myArray), "\n";''' | + | $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- | ||
Line 305: | Line 313: | ||
'''<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: ", $myArray[0], "\n"; </nowiki>''' | + | '''<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: ", $myArray[2], "\n"; </nowiki>''' | + | '''<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: ", $myArray[$#myArray], "\n";</nowiki>''' | + | '''<nowiki>print "Last element of myArray is: ", ''' |
+ | |||
+ | '''$myArray[$#myArray], "\n";</nowiki>''' | ||
Line 331: | Line 345: | ||
− | '''<nowiki>print "First element of myArray is: ", | + | '''<nowiki>print "First element of myArray is: ", </nowiki>''' |
+ | '''<nowiki> $myArray[0], "\n"; </nowiki>''' | ||
− | '''<nowiki>print "Last element of myArray is: ", $myArray[$#myArray], "\n";</nowiki>''' | + | |
+ | '''<nowiki>print "Last element of myArray is: ", </nowiki>''' | ||
+ | |||
+ | '''<nowiki>$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;"| 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- | ||
Line 396: | Line 414: | ||
'''<nowiki># Do the operation on individual element </nowiki>''' | '''<nowiki># Do the operation on individual element </nowiki>''' | ||
− | '''<nowiki>print "Printing element using for loop: ", $myArray[$i], "\n"; </nowiki>''' | + | '''<nowiki>print "Printing element using for loop: ", </nowiki>''' |
+ | |||
+ | '''<nowiki>$myArray[$i], "\n"; </nowiki>''' | ||
'''} ''' | '''} ''' | ||
Line 403: | Line 423: | ||
'''<nowiki># Used to separate the output on terminal </nowiki>''' | '''<nowiki># Used to separate the output on terminal </nowiki>''' | ||
− | '''print "=================================================\n"; ''' | + | '''print "=======================''' |
+ | |||
+ | '''==========================\n"; ''' | ||
Line 412: | Line 434: | ||
'''<nowiki># Do the operation on individual element </nowiki>''' | '''<nowiki># Do the operation on individual element </nowiki>''' | ||
− | '''print "Printing element using foreach loop: $element\n"; ''' | + | '''print "Printing element using foreach loop: ''' |
+ | |||
+ | '''$element\n"; ''' | ||
'''}''' | '''}''' | ||
Line 474: | Line 498: | ||
'''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 541: | Line 565: | ||
# 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''' | ||
− | |||
− | |||
− | |||
|- | |- |
Revision as of 10:44, 3 September 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 & Data Structures in Perl
|
Slide | * Array is a simple data structure which contains elements of any data type.
|
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 a 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
perl arrayIndex.pl
|
Now, switch to the terminal and execute the Perl script. Type -
|
Highlight the output on the terminal
|
The output shown on terminal is <<pause>>
|
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;
$#myArray + 1, "\n";
$length\n";
scalar(@myArray), "\n"; |
Type the following piece of code, as shown on the screen-
Here we have declared & defined an array which contains 5 elements.
|
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 is as shown 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
$myArray[0], "\n";
$myArray[$#myArray], "\n"; |
Please note-
i.e position of element minus 1.
zero is passed as index.
|
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"; } |
This will open loopingOverArray Perl script in gedit.
<<pause>>
|
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 following output is displayed on the terminal. <pause>
|
Slide | In Perl, we can declare a sequential array as-
|
Slide | Perl also provides array slicing.
@mynewArray = @myarray[1, 4];
@mynewArray = (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.
|