Difference between revisions of "PERL/C2/Arrays/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
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 Cue'''</center>
+
| 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 Clue'''</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 25: Line 25:
 
* '''Sequential Array'''
 
* '''Sequential Array'''
 
* '''Array Slicing'''
 
* '''Array Slicing'''
 +
 +
  
 
|-
 
|-
 
| 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 51: Line 54:
 
* 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'''.
 
* '''Array '''length expands/shrinks as and when '''elements''' are added/removed from it.
 
* '''Array '''length expands/shrinks as and when '''elements''' are added/removed from it.
 +
  
  
Line 91: Line 95:
 
'''@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”;'''
  
'''$#myArray\n”;'''
 
  
  
Line 99: Line 102:
  
 
<nowiki><<pause>></nowiki>
 
<nowiki><<pause>></nowiki>
 +
 +
  
  
Line 125: Line 130:
  
 
'''perl arrayIndex.pl'''
 
'''perl arrayIndex.pl'''
 +
  
  
Line 144: Line 150:
  
 
'''Last index of myArray is: 4'''
 
'''Last index of myArray is: 4'''
 +
 +
 +
  
 
|-
 
|-
Line 159: Line 168:
 
# Using '''PERL '''inbuilt '''scalar function; scalar(@array)'''
 
# Using '''PERL '''inbuilt '''scalar function; scalar(@array)'''
 
# Assign '''array '''to a '''scalar variable;''' '''$arrayLength = @array'''
 
# Assign '''array '''to a '''scalar variable;''' '''$arrayLength = @array'''
 +
  
  
Line 191: Line 201:
  
  
'''print "Length of an array using index: ", '''
+
'''print "Length of an array using index: ", $#myArray + 1, "\n"; '''
 
+
'''$#myArray + 1, "\n"; '''
+
 
+
 
+
'''print "Length of an array using scalar variable:
+
  
$length\n"; '''
 
  
 +
'''print "Length of an array using scalar variable: $length\n"; '''
  
'''print "Length of an array using scalar function: ", '''
 
  
'''scalar(@myArray), "\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 216: Line 220:
  
 
Highlighted here are various ways to find the length of an '''array '''in '''Perl.'''
 
Highlighted here are various ways to find the length of an '''array '''in '''Perl.'''
 +
 +
  
  
Line 230: Line 236:
 
|-
 
|-
 
| 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 246: Line 253:
 
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 is as shown on the '''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 is as shown on the '''terminal.'''
 +
 +
  
  
Line 258: Line 267:
 
|-
 
|-
 
| 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 265: Line 275:
 
# Last Position
 
# Last Position
 
# Any position
 
# Any position
 +
  
  
Line 278: Line 289:
  
 
This will open '''perlArray dot pl''' script in '''gedit'''.
 
This will open '''perlArray dot pl''' script in '''gedit'''.
 +
 +
  
  
Line 292: Line 305:
 
'''<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>'''
 
+
'''$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>'''
 
+
'''$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>'''
  
'''$myArray[$#myArray], "\n";</nowiki>'''
 
  
  
Line 323: Line 331:
  
  
'''<nowiki>print "First element of myArray is: ", </nowiki>'''
+
'''<nowiki>print "First element of myArray is: ", $myArray[0], "\n"; </nowiki>'''
 
+
'''<nowiki> $myArray[0], "\n"; </nowiki>'''
+
 
+
  
'''<nowiki>print "Last element of myArray is: ", </nowiki>'''
 
  
'''<nowiki>$myArray[$#myArray], "\n";</nowiki>'''
+
'''<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;"| 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 349: Line 353:
  
 
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 388:
 
# Using '''for loop'''
 
# Using '''for loop'''
 
# Using '''foreach loop'''
 
# Using '''foreach loop'''
 +
  
  
Line 391: Line 420:
 
'''<nowiki># Do the operation on individual element </nowiki>'''
 
'''<nowiki># Do the operation on individual element </nowiki>'''
  
'''<nowiki>print "Printing element using for loop: ", </nowiki>'''
+
'''<nowiki>print "Printing element using for loop: ", $myArray[$i], "\n"; </nowiki>'''
 
+
'''<nowiki>$myArray[$i], "\n"; </nowiki>'''
+
  
 
'''} '''
 
'''} '''
Line 400: Line 427:
 
'''<nowiki># Used to separate the output on terminal </nowiki>'''
 
'''<nowiki># Used to separate the output on terminal </nowiki>'''
  
'''print "======================='''
+
'''print "============================================\n"; '''
 
+
'''==========================\n"; '''
+
  
  
Line 411: Line 436:
 
'''<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"; '''
 
+
'''$element\n"; '''
+
  
 
'''}'''
 
'''}'''
Line 424: Line 447:
  
  
Here, we are printing each '''element''' of the '''array''' I have made this modification. Pls verify that this is correct.by iterating the '''index'''.
+
Here, we are printing each '''element''' of the '''array''' by iterating the '''index'''.
  
  
Line 442: Line 465:
 
* please go through the relevant tutorials
 
* please go through the relevant tutorials
 
* on the spoken tutorial website.  
 
* on the spoken tutorial website.  
 +
  
  
Line 453: Line 477:
  
 
'''perl scalars.pl'''
 
'''perl scalars.pl'''
 +
  
  
Line 473: Line 498:
 
'''Printing element using for loop: 3 '''
 
'''Printing element using for loop: 3 '''
  
'''<nowiki>==========================</nowiki>'''
+
'''<nowiki>==============================</nowiki>'''
  
 
'''Printing element using foreach loop: 1 '''
 
'''Printing element using foreach loop: 1 '''
Line 484: Line 509:
  
 
''<nowiki><< Will just highlight the output and will not read it while recording >></nowiki>''
 
''<nowiki><< Will just highlight the output and will not read it while recording >></nowiki>''
 +
 +
  
  
Line 494: Line 521:
 
* Similarly  
 
* Similarly  
 
* '''@numericArray = (1..5);''' is same as '''@numericArray = (1, 2, ,3, 4, 5);'''
 
* '''@numericArray = (1..5);''' is same as '''@numericArray = (1, 2, ,3, 4, 5);'''
 +
  
  
Line 504: Line 532:
  
  
'''@myarray = (19, 23, 56, 45, 87, 89); '''
+
'''@array = (19, 23, 56, 45, 87, 89); '''
  
'''<nowiki>@mynewArray = @myarray[1, 4];</nowiki>'''
+
'''<nowiki>@newArray = @myarray[1, 4];</nowiki>'''
  
  
After '''slicing''', '''mynewArray''' will look like this -
+
After '''slicing''', '''newArray''' will look like this -
  
'''@mynewArray = (23, 87);'''
+
'''@newArray = (23, 87);'''
  
 
|-
 
|-
Line 537: 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'''
 +
# Declare array as <br/> @myArray = (1..9); and then<br/> Create an array of odd numbers from above array using array slicing.
 +
 +
  
 
|-
 
|-

Revision as of 17:29, 5 October 2013

Title Of Script: Arrays in Perl

Author: Amol Brahmankar

Keywords: Array in perl video tutorial.


Visual Clue
Narration
Slide Welcome to the spoken tutorial on Arrays in Perl.
Slide: Learning Objectives In this tutorial, we will learn about;
  • Index of an array
  • Length of an array
  • Accessing elements of an array
  • Looping over an array
  • Sequential Array
  • Array Slicing


Slide: System Requirements Here I am using Ubuntu Linux12.04 operating system and Perl 5.14.2


I will also be using the gedit Text Editor.


You can use any text editor of your choice.

Slide: Prerequisites You should have basic knowledge of Variables & Data Structures in Perl


Knowledge of Comments, loops and conditional statements will be an added advantage.


Please go through the relevant spoken tutorials on the spoken tutorial website.

Slide * Array is a simple data structure which contains elements of any data type.
  • Array index starts from zero always.
  • In Perl, it is not necessary to declare the length of an array.
  • Array length expands/shrinks as and when elements are added/removed from it.


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.


This will open arrayIndex dot pl file in geditor.

gedit


#!/usr/bin/perl


@myArray = (1, 2, 3, 'abc', 10.3);

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);


$#myArray

Here we have declared & defined an array which contains 5 elements.


As array index starts from zero, the last index value will be 4


i.e number of elements, which is 5, minus 1.

Press Ctrl+S to save the file.
Switch to terminal


Type

perl arrayIndex.pl


Now, switch to the terminal and execute the Perl script. Type -


perl arrayIndex dot pl


and press Enter.

Highlight the output on the terminal


Last index of myArray is: 4

The output shown on terminal is <<pause>>


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.


These are as follows-

  1. Index of an array + 1; $#array + 1
  2. Using PERL inbuilt scalar function; scalar(@array)
  3. Assign array to a scalar variable; $arrayLength = @array


Terminal


gedit arrayLength.pl & >> Enter

Let us look at an illustration of array length using a sample program.


Switch to the terminal and type -

gedit arrayLength dot pl space ampersand


Press Enter.


This will open arrayLength Perl script in gedit.

Gedit


#!/usr/bin/perl


@myArray = (1, 2, 3, 'abc', 10.3);

$length = @myArray;


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";

Type the following piece of code, as shown on the screen-


<<pause>>

Here we have declared & defined an array which contains 5 elements.


So, output will display 5.


Highlighted here are various ways to find the length of an array in Perl.



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-


perl arrayLength dot pl and press Enter.

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 -
  1. First Position
  2. Last Position
  3. Any position


Terminal Switch to the terminal and type -


gedit perlArray dot pl space ampersand

and press Enter.


This will open perlArray dot pl script in gedit.



Gedit


#!/usr/bin/perl


@myArray = (1, 2, 3, 'abc', 10.3);


# Access the first element of an array

print "First element of myArray is: ", $myArray[0], "\n";


# Access 3rd element of an array

print "3rd Element of myArray is: ", $myArray[2], "\n";


# Access last element of an array

print "Last element of myArray is: ", $myArray[$#myArray], "\n";


Type the following piece of code as shown.


<<pause>>

Highlight


$myArray[0]


print "First element of myArray is: ", $myArray[0], "\n";


print "Last element of myArray is: ", $myArray[$#myArray], "\n";

Please note-
  1. myArray is declared with @ (at the rate) sign.
  2. But, to access an array element we need to use $ (dollar) sign.
  3. To access the element at 3rd position, we need to pass index as 2

i.e position of element minus 1.


Here, to access the first element of myArray

zero is passed as index.


To access the last element of myArray we have passed the last index to myArray.


Recall, we had learnt about this earlier.

Press ctrl + s Press Ctrl + S to save the file.
Switch to terminal


perl perlArray.pl


Then switch to the terminal and execute the Perl script as -


perl perlArray dot pl


and press Enter.

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-
  1. Using for loop
  2. Using foreach loop


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


Press Enter

Gedit


#!/usr/bin/perl


@myArray = (1, 2, 3);


# Iterate over an array using for loop

for ($i = 0; $i <= $#myArray; $i++) {

# Do the operation on individual element

print "Printing element using for loop: ", $myArray[$i], "\n";

}


# Used to separate the output on terminal

print "============================================\n";


# Iterate over an array using foreach loop

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.


Type the piece of code as shown on the screen

<<pause>>


Here, we are printing each element of the array by iterating the index.


The for loop will execute till the value of i variable reaches the last index of the array.


Here, foreach loop will be executed for each element of the array.


Once the array reaches its last element, it will exit the foreach loop.

Slide Please Note:
  • If you are not aware of for and foreach loops,
  • please go through the relevant tutorials
  • on the spoken tutorial website.


Press ctrl + s Press Ctrl + S to save the file.
Switch to terminal


perl scalars.pl


Then switch to the terminal and execute the Perl script as -


perl loopingOverArray dot pl


and press Enter.

Highlight the output on the terminal


Printing element using for loop: 1

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>


<< Will just highlight the output and will not read it while recording >>



Slide In Perl, we can declare a sequential array as-
  • @alphaArray = (a..d);
  • i.e alphaArray will contain elements 'a', 'b', 'c' and 'd'
  • Similarly
  • @numericArray = (1..5); is same as @numericArray = (1, 2, ,3, 4, 5);


Slide Perl also provides array slicing.


This is nothing but extracting part of an array and dumping it into a new array.


@array = (19, 23, 56, 45, 87, 89);

@newArray = @myarray[1, 4];


After slicing, newArray will look like this -

@newArray = (23, 87);

Slide: Summary Let us summarize.


In this tutorial, we have learnt to-

  • Find index of an array
  • Find length of an array
  • Access elements of an array
  • Loop over an array
  • Sequenial Array
  • Array Slicing

using sample programs.

Slide: Assignment Here is assignment for you -
  1. Declare an array of rainbow colors
  2. Print 4th element of this array
  3. Print Length and last index of this array
  4. Loop over each element of the array using for & foreach loops
  5. Declare array as
    @myArray = (1..9); and then
    Create an array of odd numbers from above array using array slicing.


About the Project Watch the video available at the following link


It summaries the Spoken Tutorial project


If you do not have good bandwidth, you can

download and watch it

Spoken Tutorial Workshops The Spoken Tutorial Project Team


Conducts workshops using spoken tutorials


Gives certificates to those who pass an online

test


For more details, please write to

contact at spoken hyphen tutorial dot org

Acknowledgment


http://spoken-tutorial.org\NMEICT-Intro

Spoken Tutorial Project is a part of the Talk to a

Teacher project


It is supported by the National Mission on

Education through ICT, MHRD, Government of India.


More information on this Mission is available

spoken hypen tutorial dot org slash NMEICT hyphen Intro

Hope you enjoyed this Perl tutorial.

This is Amol Brahmankar signing off.


Thanks for joining

Contributors and Content Editors

AmolBrahmankar, Nancyvarkey