PERL/C2/Data-Structures/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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>





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.

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