Difference between revisions of "PERL/C2/Data-Structures/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title Of Script: '''Data structures in Perl '''Author:''' Amol Brahmankar '''Keywords:''' Data structures in perl video tutorial. {| style="border-spacing:0;" | style="bo…')
 
Line 7: Line 7:
  
  
{| style="border-spacing:0;"
+
{| border=1
| 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>
+
!Visual Cue
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
+
!Narration
 
+
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
+
|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;"| Welcome to the spoken tutorial on '''Data Structures''' in '''Perl'''
+
|Welcome to the spoken tutorial on '''Data Structures''' 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: Learning Objectives
+
|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;
+
|In this tutorial, we will learn about '''Data Structures''' available in '''Perl'''
 
+
'''Data Structures''' available 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: System Requirements
+
|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'''
+
|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.
 
  
 
You can use any text editor of your choice.
 
You can use any text editor of your choice.
 
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites
+
|Slide: Pre-requisites
| 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 '''in''' Perl'''
+
|You should have basic knowledge of '''Variables '''in''' Perl'''
 
+
 
+
Knowledge of''' Comments, loops and conditional statements''' will be an added advantage'''.'''
+
  
 +
Knowledge of''' comments, loops and conditional statements''' will be an added advantage.
  
 
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.
 
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.
 
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
+
|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 has 3 types of data structure -
+
|'''Perl''' has 3 types of '''data structure''' -
 
+
# '''Scalar'''
1. '''Scalar'''
+
# '''Array'''
 
+
# '''Hash''', also, called as '''Associative Array'''
2. '''Array'''
+
 
+
3. '''Hash'''. Also, called as '''Associative Array'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
+
|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;"| '''Scalar:'''
+
|'''Scalar:'''
  
 
# This type of '''data structure''' holds a value of any '''data type'''.  
 
# This type of '''data structure''' holds a value of any '''data type'''.  
 
# The '''data type''' can be '''string, number, double''' etc.
 
# The '''data type''' can be '''string, number, double''' etc.
# It can also hold the reference to an array or reference to a '''hash.'''  
+
# It can also hold the reference to an '''array''' or reference to a '''hash.'''  
 
+
'''Note:''' reference in '''Perl''' will be covered in subsequent tutorial.
+
  
 +
'''Note:''' Reference in '''Perl''' will be covered in subsequent tutorial.
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
+
|Slide
 
+
  
 
'''Declaring scalar data structure;'''
 
'''Declaring scalar data structure;'''
Line 70: Line 54:
  
 
'''$string = 'I am scalar of type string';'''
 
'''$string = 'I am scalar of type string';'''
| 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;"| '''Scalar''' type of''' data structure''' is as simple as declaring the '''variable'''.  
+
|'''Scalar''' type of''' data structure''' is as simple as declaring the '''variable'''.  
 
+
  
 
'''$count = 12; '''
 
'''$count = 12; '''
 
  
 
'''$string = 'I am scalar of type string';'''
 
'''$string = 'I am scalar of type string';'''
 
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''
+
|'''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 perform the following operations on '''scalar;'''
+
|We can perform the following operations on '''scalar;'''
  
 
# Assign a value to it
 
# Assign a value to it
Line 86: Line 67:
 
# '''Arithmetic operations''' on '''number''' type of '''scalars''' like add, subtract etc
 
# '''Arithmetic operations''' on '''number''' type of '''scalars''' like add, subtract etc
 
# '''string operations''' on '''string''' '''scalar''' like '''concatenation,''' '''substr''' etc
 
# '''string operations''' on '''string''' '''scalar''' like '''concatenation,''' '''substr''' etc
 
 
 
 
|-
 
|-
| 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 look at an example of s'''calar data structure'''.
+
|Now let us look at an example of '''scalar data structure'''.
  
 
|-
 
|-
| 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 >> type gedit scalars dot pl & >> Enter.
+
|Switch to terminal >>  
 
+
Type gedit scalars dot pl & >>  
 
+
Press Enter.
 
+
|Switch to '''terminal''' and type '''gedit scalars dot 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;"| Switch to '''terminal''' and type  
+
 
+
'''gedit scalars 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;"| Gedit
+
|Gedit
 
+
  
 
<nowiki>#!/usr/bin/perl </nowiki>
 
<nowiki>#!/usr/bin/perl </nowiki>
 
  
 
<nowiki># Following are the various ways of declaring sclar </nowiki>
 
<nowiki># Following are the various ways of declaring sclar </nowiki>
Line 116: Line 87:
  
 
$string = 'I am scalar of type string';  
 
$string = 'I am scalar of type string';  
 
  
 
<nowiki>=head </nowiki>
 
<nowiki>=head </nowiki>
Line 125: Line 95:
  
 
<nowiki>=cut </nowiki>
 
<nowiki>=cut </nowiki>
 
  
 
$newCount = $count;  
 
$newCount = $count;  
  
 
print "Original Count: $newCount\n";  
 
print "Original Count: $newCount\n";  
 
  
 
$newCount = $newCount + 12; #Can be written as $newCount += 12;  
 
$newCount = $newCount + 12; #Can be written as $newCount += 12;  
Line 140: Line 108:
  
 
print "New Count After Substraction: $newCount\n";  
 
print "New Count After Substraction: $newCount\n";  
 
  
 
<nowiki>=head </nowiki>
 
<nowiki>=head </nowiki>
Line 149: Line 116:
  
 
<nowiki>=cut </nowiki>
 
<nowiki>=cut </nowiki>
 
  
 
$string = $string. ' in Perl'; #can be written as $string .= ' in Perl';  
 
$string = $string. ' in Perl'; #can be written as $string .= ' in Perl';  
  
 
print "Original String: $string\n";  
 
print "Original String: $string\n";  
 
  
 
$newString = substr($string, 0, 11);  
 
$newString = substr($string, 0, 11);  
  
 
print "New String: $newString\n";
 
print "New String: $newString\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;"| This will open the '''scalars dot pl file in gedit'''.
+
|This will open the '''scalars dot pl file in gedit'''.
  
 
Type the code as displayed on the screen.
 
Type the code as displayed on the screen.
 +
 +
  
  
 
This is the '''declaration''' and '''assignment''' to the '''scalar.'''
 
This is the '''declaration''' and '''assignment''' to the '''scalar.'''
 +
 +
  
  
 
These are few '''arithmetic operations''' that can be performed on '''number''' type of '''scalar'''  
 
These are few '''arithmetic operations''' that can be performed on '''number''' type of '''scalar'''  
 
  
 
<nowiki><pause></nowiki>
 
<nowiki><pause></nowiki>
  
  
These are '''string operations '''that can be performed on '''String''' type of '''scalar'''.
 
  
  
<nowiki><pause></nowiki>
 
  
  
'''substr''' is the '''PERL function''' which provides part of the '''string''' as output.
 
  
  
Here '''index 0 '''specifies start of a string, i.e. from where we want to start extraction of the string.
 
  
  
And 11 is the '''offset '''upto where we want the string to be in the output.
 
  
|-
 
| 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 scalars.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 scalars dot pl'''
 
  
  
and press''' Enter.'''
+
 
 +
 
 +
 
 +
 
 +
 
 +
These are '''string operations '''that can be performed on '''string''' type of '''scalar'''.
 +
 
 +
<nowiki><pause></nowiki>
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
'''substr''' is the '''PERL function''' which provides part of the '''string''' as output.
 +
 
 +
Here '''index 0 '''specifies start of a '''string''', i.e. from where we want to start extraction of the '''string'''.
 +
 
 +
And 11 is the '''offset '''upto where we want the '''string''' to be in the output.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal
+
|Press ctrl + s
 +
|Press '''ctrl + s''' to save the file.
 +
|-
 +
|Switch to terminal and type
 +
 
 +
'''perl scalars.pl'''
 +
 
 +
 
 +
 
 +
|Then switch to the '''terminal '''and execute the '''Perl script''' as
  
 +
'''perl scalars dot pl''' and press''' Enter.'''
 +
 +
|-
 +
|Highlight the output on the terminal
  
 
'''Original Count: 12 '''
 
'''Original Count: 12 '''
Line 220: Line 207:
  
 
'''New String: I am scalar '''
 
'''New String: I am scalar '''
| 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 shown on '''terminal'''<nowiki> is as highlighted. <pause></nowiki>
+
|The output shown on '''terminal''' is as highlighted. <pause>
 
+
 
+
''<nowiki><< I Wiil just highlight the output and will not read it while recording >></nowiki>''
+
 
+
  
 
'''Original Count: 12 '''
 
'''Original Count: 12 '''
Line 235: Line 218:
  
 
'''New String: I am scalar '''
 
'''New String: I am scalar '''
 
 
|-
 
|-
| 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;"| Now, let us look at '''array data structure''' in perl.
+
|Now, let us look at '''array data structure''' 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:0.05pt solid #000000;padding:0.097cm;"| '''Array''':
+
|'''Array''':
  
 
# It is a list of '''elements'''.
 
# It is a list of '''elements'''.
Line 250: Line 231:
 
# Unlike other programming languages, there is no need to declare an '''array''' or its length before using it in '''Perl'''.
 
# Unlike other programming languages, there is no need to declare an '''array''' or its length before using it in '''Perl'''.
 
# '''Perl array''', stretches or shrinks as per the '''elements''' added or removed from it
 
# '''Perl array''', stretches or shrinks as per the '''elements''' added or removed from it
 
 
 
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
+
|Slide
 
+
  
 
Highlight @variableName = (element 1, element 2, ..., element N);
 
Highlight @variableName = (element 1, element 2, ..., 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;"| The syntax to write an '''array''' is;
+
|The syntax to write an '''array''' is;
 
+
  
 
'''at the rate variable name space equal to space open bracket list of elements separated with comma close bracket semicolon'''
 
'''at the rate variable name space equal to space open bracket list of elements separated with comma close bracket semicolon'''
  
 
|-
 
|-
| 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 look at an example of '''array data structure'''.
+
|Now let us look at an example of '''array data structure'''.
 
+
 
|-
 
|-
| 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 >> type  
+
|Switch to terminal >> type '''gedit perlArray.pl &''' >> press '''Enter.'''
 
+
|Switch to '''terminal''' and type '''gedit perlArray dot pl &''' and press '''Enter'''.
gedit perlArray.pl & >> 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;"| Switch to '''terminal''' and type  
+
 
+
'''gedit 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;"| Gedit
+
|Gedit
 
+
  
 
<nowiki>#!/usr/bin/perl</nowiki>
 
<nowiki>#!/usr/bin/perl</nowiki>
 
  
 
<nowiki>#Number array</nowiki>
 
<nowiki>#Number array</nowiki>
  
 
@numArray = (98, 99, 92, 97);
 
@numArray = (98, 99, 92, 97);
 
  
 
<nowiki>#String array</nowiki>
 
<nowiki>#String array</nowiki>
  
 
@strArray = ('Perl', 'String', 'Array');
 
@strArray = ('Perl', 'String', 'Array');
 
  
 
<nowiki>#Mix array</nowiki>
 
<nowiki>#Mix array</nowiki>
  
 
@array = (98, 'StringValue', 92);
 
@array = (98, 'StringValue', 92);
| 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;"| This will open the '''perlArray dot pl file in gedit'''.
+
|This will open the '''perlArray dot pl''' file in '''gedit'''.
  
Type the following code as displayed on the screen
+
Type the following code as displayed on the screen.
  
  
This is the number array which has elements of number type
+
This is the '''number array''' which has elements of '''number''' type.
  
  
This is the string array which has elements of string type
+
This is the '''string array''' which has elements of '''string''' type.
  
  
This array has elements of both number and string type.
+
This '''array''' has elements of both '''number''' and '''string''' type.
 
+
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit
+
|Gedit
 
+
Highlight all 3 types of array
+
  
 +
Highlight all 3 types of '''array'''
  
 
print “Number Array: @numArray\n”;
 
print “Number Array: @numArray\n”;
 
  
 
print “String Array: @strArray\n”;
 
print “String Array: @strArray\n”;
 
  
 
print “Array: @array\n”;
 
print “Array: @array\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;"| This example shows the various types of '''array in Perl'''.  
+
|This example shows the various types of '''arrays''' in '''Perl'''.  
 
+
  
 
Now let us see how to print the '''array'''.
 
Now let us see how to print the '''array'''.
 
  
 
Type
 
Type
Line 333: Line 292:
 
'''print space double quote Number Array colon space at the rate numArray slash n double quote semicolon'''
 
'''print space double quote Number Array colon space at the rate numArray slash n double quote semicolon'''
  
'''Press Enter'''
+
Press '''Enter'''
 
+
Similarly print other 2 arrays as
+
  
 +
Similarly, print other 2 '''arrays''' as
  
 
'''print “String Array: @strArray\n”;'''
 
'''print “String Array: @strArray\n”;'''
 
  
 
'''print “Array: @array\n”;'''
 
'''print “Array: @array\n”;'''
 
 
|-
 
|-
| 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
+
|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
+
|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
+
|Switch to terminal and type '''perl perlArray.pl'''
 +
|Then switch to the '''terminal '''and execute the '''Perl script''' as
  
 
+
'''perl perlArray dot pl''' and press''' Enter.'''
'''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;"| Highlight the output on the terminal
+
|Highlight the output on the terminal
 
+
  
 
'''Number Array: 98 99 92 97 '''
 
'''Number Array: 98 99 92 97 '''
Line 372: Line 317:
  
 
'''Array: 98 StringValue 92 '''
 
'''Array: 98 StringValue 92 '''
| 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>
+
|The following output is displayed on the '''terminal''' <pause>
 
+
 
+
''<nowiki><< I Wiil just highlight the output and will not read it while recording >></nowiki>''
+
 
+
  
 
'''Number Array: 98 99 92 97 '''
 
'''Number Array: 98 99 92 97 '''
Line 383: Line 324:
  
 
'''Array: 98 StringValue 92 '''
 
'''Array: 98 StringValue 92 '''
 
 
|-
 
|-
| 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;"| Now, let us look at '''Hash data structure''' in '''Perl'''.
+
|Now, let us look at '''Hash data structure''' 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:0.05pt solid #000000;padding:0.097cm;"| '''Hash''':
+
|'''Hash''':
  
# '''Hash''' is alternatively called as''' Associative array. '''
+
# '''Hash''' is alternatively called as ''' Associative array. '''
 
# It is a '''Key Value pair data structure'''.
 
# It is a '''Key Value pair data structure'''.
# '''Key in hash''' is unique.  
+
# '''Key''' in '''hash''' is unique.  
 
# If the same '''key''' is added again, then the '''value''' of that '''key''' will be overridden by the latest '''value''' assigned to the '''key.'''
 
# If the same '''key''' is added again, then the '''value''' of that '''key''' will be overridden by the latest '''value''' assigned to the '''key.'''
 
# '''Value''' can be duplicate.
 
# '''Value''' can be duplicate.
 
# It also holds '''value''' of any '''data type'''.
 
# It also holds '''value''' of any '''data type'''.
 
 
 
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
+
|Slide
 
+
  
 
Highlight  
 
Highlight  
Line 414: Line 350:
  
 
);
 
);
| 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 of '''hash''' is;
+
|The syntax of '''hash''' is;
 
+
  
 
'''percentage variable name space equal to space open bracket '''
 
'''percentage variable name space equal to space open bracket '''
Line 432: Line 367:
  
 
|-
 
|-
| 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 look at an example of '''hash data structure'''.
+
|Now let us look at an example of '''hash data structure'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit
+
|Gedit
| 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;"| Switch to '''terminal''' and type  
+
|Switch to '''terminal''' and type  
  
'''gedit perlHash dot pl &'''
+
'''gedit perlHash dot pl &''' and press '''Enter'''.
 
+
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;"| Gedit
+
|Gedit
 
+
  
 
<nowiki>#!/usr/bin/perl</nowiki>
 
<nowiki>#!/usr/bin/perl</nowiki>
 
  
 
use Data::Dumper;
 
use Data::Dumper;
 
  
 
<nowiki>#Hash of marks obtain in a subject</nowiki>
 
<nowiki>#Hash of marks obtain in a subject</nowiki>
Line 462: Line 392:
  
 
);
 
);
| 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;"| This will open the '''perlHash dot pl file in gedit'''.
+
|This will open the '''perlHash dot pl''' file in '''gedit'''.
 +
 
 +
Type the following code as displayed on the screen.
  
Type the following code as displayed on the screen;
 
  
  
 
This '''hash''' indicates the marks obtained in a subject.
 
This '''hash''' indicates the marks obtained in a subject.
  
|-
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit
 
  
 +
 +
 +
 +
 +
|-
 +
|Gedit
  
 
Highlight  
 
Highlight  
  
 
print "Hash: ", Dumper \%hash;
 
print "Hash: ", Dumper \%hash;
| 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;"| This example, shows the use of '''hash'''.  
+
|This example, shows the use of '''hash'''.  
 
+
  
 
Now let us see how to print the '''hash'''.
 
Now let us see how to print the '''hash'''.
 
  
 
For now, just note the way I have printed the '''hash'''.  
 
For now, just note the way I have printed the '''hash'''.  
 
  
 
Detailed explanation will be given in a subsequent tutorial.  
 
Detailed explanation will be given in a subsequent tutorial.  
  
 
|-
 
|-
| 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
+
|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
+
|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
+
|Switch to terminal and type
 
+
  
 
'''perl perlHash.pl'''
 
'''perl perlHash.pl'''
 +
|Then switch to the '''terminal '''and execute the '''Perl script''' as
  
 
+
'''perl perlHash dot 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;"| Then switch to the '''terminal '''and execute''' '''the '''Perl script''' as''' '''
+
 
+
 
+
'''perl perlHash 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;"| Highlight the output on the terminal
+
|Highlight the output on the terminal
 
+
  
 
'''Hash: '''
 
'''Hash: '''
Line 520: Line 443:
  
 
'''};'''
 
'''};'''
| 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>
+
|The following output is displayed on the '''terminal'''. <pause>
 
+
 
+
''<nowiki><< I Wiil just highlight the output and will not read it while recording >></nowiki>''
+
  
  
Line 535: Line 455:
  
 
'''}; '''
 
'''}; '''
 
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Summary
+
|Slide: Summary
| 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 summarize.
+
|Let us summarize.
  
 
In this tutorial, we have learnt -
 
In this tutorial, we have learnt -
 
 
* '''scalar'''
 
* '''scalar'''
 
* '''Array and'''
 
* '''Array and'''
 
* '''Hash Data Structure''' in '''Perl'''  
 
* '''Hash Data Structure''' in '''Perl'''  
 
* using sample programs.
 
* using sample programs.
 
 
 
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Assignment
+
|Slide: Assignment
| 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 -
+
|Here is assignment for you -
  
 
# Declare '''scalar''' '''variable'''  
 
# Declare '''scalar''' '''variable'''  
Line 563: Line 478:
  
 
'Department' => 'Engineering'  
 
'Department' => 'Engineering'  
 
 
 
 
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project
+
|About the Project
| 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;"| Watch the video available at the following link
+
|Watch the video available at the following link
 
+
  
 
It summaries the Spoken Tutorial project
 
It summaries the Spoken Tutorial project
  
 
+
If you do not have good bandwidth, you can download and watch it.
If you do not have good bandwidth, you can
+
 
+
download and watch it
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops
+
|Spoken Tutorial Workshops
| 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 Spoken Tutorial Project Team  
+
|The Spoken Tutorial Project Team  
 
+
  
 
Conducts workshops using spoken tutorials
 
Conducts workshops using spoken tutorials
  
 +
Gives certificates to those who pass an online test
  
Gives certificates to those who pass an online
+
For more details, please write to '''contact at spoken hyphen tutorial dot org'''
 
+
test
+
 
+
 
+
For more details, please write to
+
 
+
contact at spoken hyphen tutorial dot org
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment
+
|Acknowledgment
 
+
  
 
http://spoken-tutorial.org\NMEICT-Intro
 
http://spoken-tutorial.org\NMEICT-Intro
| 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;"| Spoken Tutorial Project is a part of the Talk to a  
+
|Spoken Tutorial Project is a part of the Talk to a Teacher project
  
Teacher project
+
It is supported by the National Mission on Education through ICT, MHRD, Government of India.
  
  
It is supported by the National Mission on
+
More information on this Mission is available '''spoken hyphen tutorial dot org slash NMEICT hyphen Intro'''
 
+
Education through ICT, MHRD, Government of India.
+
 
+
 
+
More information on this Mission is available
+
 
+
spoken hyphen tutorial dot org slash NMEICT hyphen Intro
+
  
 
|-
 
|-
| 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;"| Hope you enjoyed this '''Perl''' tutorial.  
+
|Hope you enjoyed this '''Perl''' tutorial.  
  
 
This is Amol Brahmankar signing off.
 
This is Amol Brahmankar signing off.
 
  
 
Thanks for joining
 
Thanks for joining
  
 
|}
 
|}

Revision as of 17:15, 8 October 2013

Title Of Script: Data structures in Perl

Author: Amol Brahmankar

Keywords: Data structures in perl video tutorial.


Visual Cue Narration
Slide Welcome to the spoken tutorial on Data Structures in Perl
Slide: Learning Objectives In this tutorial, we will learn about Data Structures available in Perl
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: Pre-requisites You should have basic knowledge of Variables 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 Perl has 3 types of data structure -
  1. Scalar
  2. Array
  3. Hash, also, called as Associative Array
Slide Scalar:
  1. This type of data structure holds a value of any data type.
  2. The data type can be string, number, double etc.
  3. It can also hold the reference to an array or reference to a hash.

Note: Reference in Perl will be covered in subsequent tutorial.

Slide

Declaring scalar data structure;

$count = 12;

$string = 'I am scalar of type string';

Scalar type of data structure is as simple as declaring the variable.

$count = 12;

$string = 'I am scalar of type string';

Slide We can perform the following operations on scalar;
  1. Assign a value to it
  2. Assign one scalar to another
  3. Arithmetic operations on number type of scalars like add, subtract etc
  4. string operations on string scalar like concatenation, substr etc
Now let us look at an example of scalar data structure.
Switch to terminal >>

Type gedit scalars dot pl & >> Press Enter.

Switch to terminal and type gedit scalars dot pl & and press Enter.
Gedit

#!/usr/bin/perl

# Following are the various ways of declaring sclar

$count = 12;

$string = 'I am scalar of type string';

=head

Few of the arithmatic operations that

can be performed on the sclars

=cut

$newCount = $count;

print "Original Count: $newCount\n";

$newCount = $newCount + 12; #Can be written as $newCount += 12;

print "New Count After Addition: $newCount\n";


$newCount = $newCount - 12; #Can be written as $newCount -= 12;

print "New Count After Substraction: $newCount\n";

=head

Few of the string operations that

can be performed on the sclars

=cut

$string = $string. ' in Perl'; #can be written as $string .= ' in Perl';

print "Original String: $string\n";

$newString = substr($string, 0, 11);

print "New String: $newString\n";

This will open the scalars dot pl file in gedit.

Type the code as displayed on the screen.



This is the declaration and assignment to the scalar.



These are few arithmetic operations that can be performed on number type of scalar

<pause>














These are string operations that can be performed on string type of scalar.

<pause>




substr is the PERL function which provides part of the string as output.

Here index 0 specifies start of a string, i.e. from where we want to start extraction of the string.

And 11 is the offset upto where we want the string to be in the output.

Press ctrl + s Press ctrl + s to save the file.
Switch to terminal and type

perl scalars.pl


Then switch to the terminal and execute the Perl script as

perl scalars dot pl and press Enter.

Highlight the output on the terminal

Original Count: 12

New Count After Addition: 24

New Count After Substraction: 12

String: I am scalar of type string in Perl

New String: I am scalar

The output shown on terminal is as highlighted. <pause>

Original Count: 12

New Count After Addition: 24

New Count After Substraction: 12

String: I am scalar of type string in Perl

New String: I am scalar

Now, let us look at array data structure in PERL.
Slide Array:
  1. It is a list of elements.
  2. Elements can be string, number etc.
  3. It has an index, which is used for performing various operations on the array.
  4. Index starts with zero.
  5. Unlike other programming languages, there is no need to declare an array or its length before using it in Perl.
  6. Perl array, stretches or shrinks as per the elements added or removed from it
Slide

Highlight @variableName = (element 1, element 2, ..., element N);

The syntax to write an array is;

at the rate variable name space equal to space open bracket list of elements separated with comma close bracket semicolon

Now let us look at an example of array data structure.
Switch to terminal >> type gedit perlArray.pl & >> press Enter. Switch to terminal and type gedit perlArray dot pl & and press Enter.
Gedit

#!/usr/bin/perl

#Number array

@numArray = (98, 99, 92, 97);

#String array

@strArray = ('Perl', 'String', 'Array');

#Mix array

@array = (98, 'StringValue', 92);

This will open the perlArray dot pl file in gedit.

Type the following code as displayed on the screen.


This is the number array which has elements of number type.


This is the string array which has elements of string type.


This array has elements of both number and string type.

Gedit

Highlight all 3 types of array

print “Number Array: @numArray\n”;

print “String Array: @strArray\n”;

print “Array: @array\n”;

This example shows the various types of arrays in Perl.

Now let us see how to print the array.

Type

print space double quote Number Array colon space at the rate numArray slash n double quote semicolon

Press Enter

Similarly, print other 2 arrays as

print “String Array: @strArray\n”;

print “Array: @array\n”;

Press ctrl + s Press Ctrl + S to save the file.
Switch to terminal and type perl perlArray.pl Then switch to the terminal and execute the Perl script as

perl perlArray dot pl and press Enter.

Highlight the output on the terminal

Number Array: 98 99 92 97

String Array: Perl String Array

Array: 98 StringValue 92

The following output is displayed on the terminal <pause>

Number Array: 98 99 92 97

String Array: Perl String Array

Array: 98 StringValue 92

Now, let us look at Hash data structure in Perl.
Slide Hash:
  1. Hash is alternatively called as Associative array.
  2. It is a Key Value pair data structure.
  3. Key in hash is unique.
  4. If the same key is added again, then the value of that key will be overridden by the latest value assigned to the key.
  5. Value can be duplicate.
  6. It also holds value of any data type.
Slide

Highlight

%variableName = (

'Key1' => 'Value1' ,

'Key2' => 'Value2'

);

The syntax of hash is;

percentage variable name space equal to space open bracket

Press Enter

single quote key Name single quote space equal to greater than sign space Value comma

Press Enter

single quote key Name single quote space equal to greater than sign space Value

Press Enter

close bracket semicolon

Now let us look at an example of hash data structure.
Gedit Switch to terminal and type

gedit perlHash dot pl & and press Enter.

Gedit

#!/usr/bin/perl

use Data::Dumper;

#Hash of marks obtain in a subject

%hash = (

'Subject' => 'Math',

'Marks' => 98

);

This will open the perlHash dot pl file in gedit.

Type the following code as displayed on the screen.


This hash indicates the marks obtained in a subject.




Gedit

Highlight

print "Hash: ", Dumper \%hash;

This example, shows the use of hash.

Now let us see how to print the hash.

For now, just note the way I have printed the hash.

Detailed explanation will be given in a subsequent tutorial.

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

perl perlHash.pl

Then switch to the terminal and execute the Perl script as

perl perlHash dot pl and press Enter.

Highlight the output on the terminal

Hash:

$VAR1 = {

'Subject' => 'Math',

'Marks' => 98

};

The following output is displayed on the terminal. <pause>


Hash:

$VAR1 = {

'Subject' => 'Math',

'Marks' => 98

};

Slide: Summary Let us summarize.

In this tutorial, we have learnt -

  • scalar
  • Array and
  • Hash Data Structure in Perl
  • using sample programs.
Slide: Assignment Here is assignment for you -
  1. Declare scalar variable
  2. Assign value of type float to it and then print it.
  3. Declare and print an array of colors 'Red', 'Yellow' and 'Green'.
  4. Declare and print a hash of Employee Name and their department.

Hint:

'Employee' => 'John',

'Department' => 'Engineering'

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 hyphen 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