PERL/C2/Data-Structures/English
Title Of Script: Data structures in Perl
Author: Amol Brahmankar
Keywords: Data structures in perl video tutorial.
|
|
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
|
Slide: Prerequisites | You should have basic knowledge of Variables in Perl
|
Slide | Perl has 3 types of data structure -
1. Scalar 2. Array 3. Hash. Also, called as Associative Array |
Slide | Scalar:
Note: reference in Perl will be covered in subsequent tutorial. |
Slide
$count = 12; $string = 'I am scalar of type string'; |
Scalar type of data structure is as simple as declaring the variable.
|
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 & >> Enter.
|
Switch to terminal and type
gedit scalars dot pl & and Press Enter. |
Gedit
$count = 12; $string = 'I am scalar of type string';
Few of the arithmatic operations that can be performed on the sclars =cut
print "Original Count: $newCount\n";
print "New Count After Addition: $newCount\n";
print "New Count After Substraction: $newCount\n";
Few of the string operations that can be performed on the sclars =cut
print "Original String: $string\n";
print "New String: $newString\n"; |
This will open the scalars dot pl file in gedit.
Type the code as displayed on the screen.
|
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
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>
New Count After Addition: 24 New Count After Substraction: 12 String: I am scalar of type string in Perl New String: I am scalar |
Slide | Now, let us look at array data structure in perl. |
Array:
| |
Slide
|
The syntax to write an array is;
|
Now let us look at an example of array data structure. | |
Switch to terminal >> type
gedit perlArray.pl & >> Enter. |
Switch to terminal and type
gedit perlArray dot pl & and Press Enter. |
Gedit
@numArray = (98, 99, 92, 97);
@strArray = ('Perl', 'String', '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
|
This example shows the various types of array in Perl.
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
|
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
String Array: Perl String Array Array: 98 StringValue 92 |
The following output is displayed on the terminal <pause>
String Array: Perl String Array Array: 98 StringValue 92 |
Slide | Now, let us look at Hash data structure in Perl. |
Hash:
| |
Slide
%variableName = ( 'Key1' => 'Value1' , 'Key2' => 'Value2' ); |
The syntax of hash is;
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
%hash = ( 'Subject' => 'Math', 'Marks' => 98 ); |
This will open the perlHash dot pl file in gedit.
Type the following code as displayed on the screen;
|
Gedit
print "Hash: ", Dumper \%hash; |
This example, shows the use of hash.
|
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
$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
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 hyphen tutorial dot org slash NMEICT hyphen Intro |
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|