Difference between revisions of "PERL/C2/Data-Structures/English"
Nancyvarkey (Talk | contribs) |
Nancyvarkey (Talk | contribs) |
||
Line 209: | Line 209: | ||
|The output shown on '''terminal''' is as highlighted. <pause> | |The output shown on '''terminal''' is as highlighted. <pause> | ||
− | |||
− | |||
− | |||
− | |||
− | + | ||
+ | |||
+ | |||
|- | |- | ||
| | | | ||
Line 286: | Line 284: | ||
|This example shows the various types of '''arrays''' in '''Perl'''. | |This example shows the various types of '''arrays''' in '''Perl'''. | ||
− | + | This is how we can print the '''array''' in '''Perl'''. | |
+ | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
|Press ctrl + s | |Press ctrl + s | ||
Line 319: | Line 312: | ||
|The following output is displayed on the '''terminal''' <pause> | |The following output is displayed on the '''terminal''' <pause> | ||
− | |||
− | |||
− | + | ||
+ | |||
+ | |||
+ | |||
+ | |||
|- | |- | ||
| | | | ||
Line 354: | Line 349: | ||
'''percentage variable name space equal to space open bracket ''' | '''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''' | '''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''' | '''single quote key Name single quote space equal to greater than sign space Value''' | ||
− | ''' | + | Press '''Enter''' |
'''close bracket semicolon''' | '''close bracket semicolon''' | ||
Line 461: | Line 456: | ||
In this tutorial, we have learnt - | In this tutorial, we have learnt - | ||
* '''scalar''' | * '''scalar''' | ||
− | * '''Array | + | * '''Array''' and |
* '''Hash Data Structure''' in '''Perl''' | * '''Hash Data Structure''' in '''Perl''' | ||
* using sample programs. | * using sample programs. |
Latest revision as of 17:28, 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>
|
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.
This is how we can print the array in Perl.
|
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>
|
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 |