Difference between revisions of "PERL/C2/Data-Structures/English"
(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…') |
Nancyvarkey (Talk | contribs) |
||
Line 7: | Line 7: | ||
− | {| | + | {| border=1 |
− | + | !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''' | |
− | + | ||
− | '''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. | 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. | ||
− | |||
|- | |- | ||
− | | | + | |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. | Please go through the relevant spoken tutorials on the '''spoken tutorial '''website. | ||
− | |||
|- | |- | ||
− | + | |Slide | |
− | | | + | |'''Perl''' has 3 types of '''data structure''' - |
− | + | # '''Scalar''' | |
− | + | # '''Array''' | |
− | + | # '''Hash''', also, called as '''Associative Array''' | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | + | |Slide | |
− | + | |'''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. | ||
|- | |- | ||
− | + | |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';''' | ||
− | + | |'''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';''' | ||
− | |||
|- | |- | ||
− | + | |'''Slide''' | |
− | + | |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 | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | + | | | |
− | + | |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 scalars dot pl &''' | + | |
− | + | ||
− | and | + | |
|- | |- | ||
− | + | |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"; | ||
− | + | |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. | ||
|- | |- | ||
− | | | + | |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 ''' | ||
− | + | |The output shown on '''terminal''' is as highlighted. <pause> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
'''Original Count: 12 ''' | '''Original Count: 12 ''' | ||
Line 235: | Line 218: | ||
'''New String: I am scalar ''' | '''New String: I am scalar ''' | ||
− | |||
|- | |- | ||
− | | | + | | |
− | + | |Now, let us look at '''array data structure''' in '''PERL'''. | |
− | + | ||
|- | |- | ||
− | | | + | |Slide |
− | + | |'''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 | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | + | |Slide | |
− | + | ||
Highlight @variableName = (element 1, element 2, ..., element N); | 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''' | '''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 perlArray.pl & >> Enter. | + | |
− | + | ||
− | + | ||
− | '''gedit perlArray dot pl &''' | + | |
− | + | ||
− | and | + | |
|- | |- | ||
− | + | |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); | ||
− | + | |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. |
− | + | ||
|- | |- | ||
− | + | |Gedit | |
− | + | ||
− | + | ||
+ | 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”; | ||
− | + | |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''' |
− | + | ||
− | + | ||
+ | Similarly, print other 2 '''arrays''' as | ||
'''print “String Array: @strArray\n”;''' | '''print “String Array: @strArray\n”;''' | ||
− | |||
'''print “Array: @array\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.''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | '''perl perlArray dot pl''' | + | |
− | + | ||
− | + | ||
− | and press''' Enter.''' | + | |
|- | |- | ||
− | + | |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 ''' | ||
− | + | |The following output is displayed on the '''terminal''' <pause> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
'''Number Array: 98 99 92 97 ''' | '''Number Array: 98 99 92 97 ''' | ||
Line 383: | Line 324: | ||
'''Array: 98 StringValue 92 ''' | '''Array: 98 StringValue 92 ''' | ||
− | |||
|- | |- | ||
− | | | + | | |
− | + | |Now, let us look at '''Hash data structure''' in '''Perl'''. | |
|- | |- | ||
− | | | + | |Slide |
− | + | |'''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'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | + | |Slide | |
− | + | ||
Highlight | Highlight | ||
Line 414: | Line 350: | ||
); | ); | ||
− | + | |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: | ||
|- | |- | ||
− | + | | | |
− | + | |Now let us look at an example of '''hash data structure'''. | |
|- | |- | ||
− | + | |Gedit | |
− | + | |Switch to '''terminal''' and type | |
− | '''gedit perlHash dot pl &''' | + | '''gedit perlHash dot pl &''' and press '''Enter'''. |
− | + | ||
− | and | + | |
|- | |- | ||
− | + | |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: | ||
); | ); | ||
− | + | |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. | This '''hash''' indicates the marks obtained in a subject. | ||
− | |||
− | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |- | ||
+ | |Gedit | ||
Highlight | Highlight | ||
print "Hash: ", Dumper \%hash; | print "Hash: ", Dumper \%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. | ||
|- | |- | ||
− | + | |Press ctrl + s | |
− | + | |Press '''Ctrl + S '''to save the file. | |
|- | |- | ||
− | + | |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.''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | '''perl perlHash dot pl''' | + | |
− | + | ||
− | + | ||
− | and press''' Enter.''' | + | |
|- | |- | ||
− | + | |Highlight the output on the terminal | |
− | + | ||
'''Hash: ''' | '''Hash: ''' | ||
Line 520: | Line 443: | ||
'''};''' | '''};''' | ||
− | + | |The following output is displayed on the '''terminal'''. <pause> | |
− | + | ||
− | + | ||
− | + | ||
Line 535: | Line 455: | ||
'''}; ''' | '''}; ''' | ||
− | |||
|- | |- | ||
− | + | |Slide: Summary | |
− | + | |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. | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | + | |Slide: Assignment | |
− | + | |Here is assignment for you - | |
# Declare '''scalar''' '''variable''' | # Declare '''scalar''' '''variable''' | ||
Line 563: | Line 478: | ||
'Department' => 'Engineering' | 'Department' => 'Engineering' | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | + | |About the Project | |
− | + | |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 | + | |
|- | |- | ||
− | + | |Spoken Tutorial Workshops | |
− | + | |The Spoken Tutorial Project Team | |
− | + | ||
Conducts workshops using spoken tutorials | 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''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | For more details, please write to | + | |
− | + | ||
− | contact at spoken hyphen tutorial dot org | + | |
|- | |- | ||
− | + | |Acknowledgment | |
− | + | ||
http://spoken-tutorial.org\NMEICT-Intro | 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''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | 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. | 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 -
|
Slide | Scalar:
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;
|
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";
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.
<pause>
<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:
|
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.
|
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:
|
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>
$VAR1 = { 'Subject' => 'Math', 'Marks' => 98 }; |
Slide: Summary | Let us summarize.
In this tutorial, we have learnt -
|
Slide: Assignment | Here is assignment for you -
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 | 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.
|
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off. Thanks for joining |