Difference between revisions of "PERL/C2/Array-functions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title Of Script: '''Array functions in Perl '''Author:''' Amol Brahmankar '''Keywords:''' Array functions in perl video tutorial. {| style="border-spacing:0;" | style="bo…')
 
Line 65: Line 65:
 
This can be done by using;
 
This can be done by using;
  
# '''pop''' '''function''' which removes an '''element''' from the last position of an '''Array'''
 
 
# '''push''' '''function''' which adds an '''element''' at the last position of an '''Array'''  
 
# '''push''' '''function''' which adds an '''element''' at the last position of an '''Array'''  
 +
# '''pop''' '''function''' which removes an '''element''' from the last position of an '''Array'''
  
  
Line 137: Line 137:
  
  
| 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 me explain the syntax of '''push function-'''
+
| 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;"| '''push function''' takes 2 arguments-
  
  
# '''1st argument '''to the '''push function '''is the '''Array''' in which to add an''' element.'''
+
# '''1st argument '''to the '''push function, '''is the '''Array''' in which to add an''' element.'''
 
# '''2nd argument '''is the''' element '''which is''' '''to be '''pushed''' into the '''Array.'''
 
# '''2nd argument '''is the''' element '''which is''' '''to be '''pushed''' into the '''Array.'''
  
Line 150: Line 150:
  
 
* '''pop function''' takes only one''' argument'''
 
* '''pop function''' takes only one''' argument'''
* It is the '''Arrayname '''from which an '''element '''needs to be removed.
+
* It is the '''Array '''from which an '''element '''needs to be removed.
  
  
Line 158: Line 158:
 
| 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;"| '''Note:'''
 
| 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;"| '''Note:'''
  
Both these functions works at '''last position''' of an '''Array.'''
+
Both these functions work at '''last position''' of an '''Array.'''
  
 
|-
 
|-

Revision as of 12:41, 29 August 2013

Title Of Script: Array functions in Perl

Author: Amol Brahmankar

Keywords: Array functions in perl video tutorial.


Visual Clue
Narration
Slide Welcome to the spoken tutorial on Array Functions in Perl
Slide: Learning Objectives In this tutorial, we will learn about Array functions in Perl, like
  1. push
  2. pop
  3. shift
  4. unshift
  5. split
  6. splice
  7. join
  8. sort
  9. qw


Slide: System Requirements 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: Prerequisites You should have basic knowledge of Variables, Data Structures & Arrays 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 provides certain inbuilt functions.


These functions can perform various operations on an Array.

Slide Let us first learn how to add and remove elements from last position of an Array.


This can be done by using;

  1. push function which adds an element at the last position of an Array
  2. pop function which removes an element from the last position of an Array


Terminal


Let us understand push and pop functions by using a sample program.


Open the terminal and type gedit perlArray dot pl space ampersand


Press Enter

Gedit


#!/usr/bin/perl


@myArray = (1, 2, 3);


print "Original array: @myArray\n";


# Insert an element at the last position of an array

push (@myArray, 4);


print "New array after pushing 4 into an array: @myArray\n";


# Remove an element from the last position of an array

pop (@myArray);

print "New array after popping element: @myArray\n";

This will open perlArray dot pl file in gedit.


Type the code as shown on the screen

<<pause>>


Here, we have defined an Array of length 3.


push function will insert an element at the last position of this Array i.e after 3;


wheras, pop function will remove an element from the last position of the Array.


In our case, 4 will be removed from the Array.

Press Ctrl + S to save the file.
Slide


push function takes 2 arguments-


  1. 1st argument to the push function, is the Array in which to add an element.
  2. 2nd argument is the element which is to be pushed into the Array.


Slide The syntax of pop function is as follows -
  • pop function takes only one argument
  • It is the Array from which an element needs to be removed.


Slide Note:

Both these functions work at last position of an Array.

Slide The element removed by pop function can be collected into another variable.


The syntax for this is -

$variable = pop(@myArray)

Switch to terminal


perl perlArray.pl


Now switch to the terminal and execute the Perl script.


Type perl perlArray dot pl and press Enter.

Highlight the output on the terminal


Original array: 1 2 3

New array after pushing 4 into an array: 1 2 3 4

New array after popping element: 1 2 3

The output is shown on the terminal.


Original array

New array after pushing 4 into an array

New array after popping element



Slide Now, let us see how to add/remove an element from the 1st position of an Array.


This can be achieved using-

  1. unshift function - which adds an element to an Array at the 1st position
  2. shift function - which removes the
    first element from an Array.


Gedit Let us understand this using a sample program.


I will open perlArray dot pl file which I have already created.

Gedit


# Insert an element at the first position of an array

unshift (@myArray, 0);


print "Array after adding 0 into an array at 1st position: @myArray\n";


# Remove an element from the first position of an array

shift (@myArray);


print "Array after removing element from 1st position: @myArray\n";

Type the following piece of code as shown on the screen.


unshift function will insert an element at the first position

i.e before 1


shift function will remove an element from the first position.


In our case, zero will be removed.

Press Ctrl + S to save the file.
Slide


The unshift function takes 2 arguments -
  1. 1st argument is the Array in which to add an element
  2. 2nd argument is the element to be added into the Array


Slide shift function takes only one argument -
  • This is the Array from which the element needs to be removed.


Slide Note:

Both these functions works at first position of an array.



Slide We can collect the element removed by shift function into some variable.


This syntax for this is -

$variable = shift(@myArray)

Switch to terminal


perl perlArray.pl


Then switch to the terminal and execute the Perl script.


Type perl perlArray dot pl and press Enter.

Highlight the output on the terminal


Array after adding 0 into an array at 1st position: 0 1 2 3

Array after removing element from 1st position: 1 2 3


The output displayed on the terminal is as highlighted.
Now, let us see how to remove an element from a specified position of an Array.
slide * splice function removes an element from a specified position of an Array.
  • The return value of this function is an Array of removed elements.


Gedit Let us understand this using a sample program.


Go to the perlArray dot pl file which we created earlier.

# Removes element(s) from the specified position

@myNewArray = (1, 2, 3, 4, 5, 6, 7, 8, 9);

@splicedArray = splice(@myNewArray, 4, 2);


print "Spliced Array: @splicedArray\n";

Type the piece of code shown on the screen.


We need to provide

  • the index from where we want to remove the elements and
  • the offset upto which we want to remove the elements

In our case, the elements 5 and 6 will be removed.

Switch to terminal


perl perlArray.pl


Then switch to the terminal and execute the Perl script by typing -


perl perlArray dot pl and press Enter.

Highlight the output on the terminal


Spliced Array: 5 6

The output displayed on the terminal is as highlighted.
Now let us look at few other inbuilt functions of Arrays.
Slide * split function is used to divide a string at a specified delimiter.
  • The return value of this function is an Array.
  • The elements of this Array are the divided portions of the string.


Slide * join function joins the elements of an Array, using the specified delimiter.
  • It returns a string of joined elements.


Slide sort function sorts an Array in alphabetical/numerical order.
Slide qw function returns an Array of words, separated by a white space.
Now let us understand all these functions using a sample program.
Terminal


gedit arrayFunctions.pl &

Switch to terminal and type


gedit arrayFunctions dot pl space ampersand


This will open arrayFunctions dot pl file in gedit.



Gedit


#!/usr/bin/perl

use Data::Dumper;


$string = "Illustration Of Array Functions";

@newArray = split (/ /, $string);


print "Declared String: $string\n\n";

print "Array after split: \n";

print Dumper \@newArray;

print "\n";


$joinedString = join(", ", @newArray);

print "String After Join: $joinedString\n\n";


@sortedArray = sort(@newArray);

print "Array after sorting: @sortedArray\n\n";


@qwArray = qw (Hello Perl Array);

print "Array after qw: \n";

print Dumper \@qwArray;


Type the following piece of code as shown.


<<pause>>


In this case, each word of the variable string will become an element of an Array.


Here, each element of newArray will be joined by comma.


sort function will sort the elements of newArray in alphabetical order.


qw function creates an Array of words separated by space.

Slide Let us understand each function.
Slide split function takes two arguments.
  • 1st is the delimiter by which the string needs to be split
  • 2nd is the string which needs to be split.

Delimiters can be specified in forward slash, single or double quotes.

Slide join function takes 2 arguments.
  1. 1st is the delimiter by which the Array elements needs to be joined.
  2. 2nd is the Array.

Delimiters can be specified in single or double

quotes.

Slide sort function takes a single argument, which is the Array that needs to be sorted.
Slide qw function returns an Array of words, separated by space.

It is not necessary to specify the word in quotes, if written using qw.

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


perl arrayFunctions.pl


Then switch to the terminal and execute the Perl script by typing -


perl arrayFunctions dot pl


and press Enter.

Highlight the output on the terminal


Declared String: Illustration Of Array Functions


Array after split:

$VAR1 = [

'Illustration',

'Of',

'Array',

'Functions'

];


String After Join: Illustration, Of, Array, Functions


Array after sorting: Array Functions Illustration Of


Array after qw:

$VAR1 = [

'Hello',

'Perl',

'Array'

];

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


<< Will just highlight the output and will not read it while recording >>



Slide: Summary Let us summarize.

In this tutorial, we have learnt to -

  • add/remove elements from an Array
  • basic functions which can be performed on Array
  • using sample programs


Slide: Assignment Here is assignment for you -
  1. 'script.spoken-tutorial.org/index.php/Perl' split the above string at '/' delimiter
  2. Add https:// at the start of an newly created Array
  3. Remove element “Perl” from an Array
  4. Declare number Array and sort it


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 hypen 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, PoojaMoolya