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…')
 
 
(2 intermediate revisions by 2 users not shown)
Line 8: Line 8:
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center>
+
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center>
 
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
 
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
  
Line 53: Line 53:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
| 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;"| Perl provides certain inbuilt '''functions'''.
+
| 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;"| '''Perl''' provides certain inbuilt '''functions'''.
  
  
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 75: Line 73:
  
  
| 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 us understand '''push''' and '''pop''' '''functions''' by using a sample program.
+
| 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 us understand '''push''' and '''pop functions''' by using a sample program.
  
  
Line 120: Line 118:
  
  
'''push''' '''function''' will insert an '''element''' at the last position of this '''Array''' i.e after 3<nowiki>;</nowiki>
+
'''push function''' will insert an '''element''' at the last position of this '''Array''' i.e after 3<nowiki>;</nowiki>
  
  
wheras, '''pop function '''will remove an '''element''' from the last position of the '''Array'''.
+
whereas, '''pop function '''will remove an '''element''' from the last position of the '''Array'''.
  
  
Line 137: Line 135:
  
  
| 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 148:
  
 
* '''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 155:
 
| 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.'''
  
 
|-
 
|-
Line 176: Line 173:
  
  
 
+
| 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;"| Now switch to the '''terminal '''and execute the '''Perl script'''.''' '''
| 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;"| Now switch to the '''terminal '''and '''execute '''the '''Perl script'''.''' '''
+
  
  
Line 199: Line 195:
  
 
'''New array after popping element'''
 
'''New array after popping element'''
 
 
  
  
Line 211: Line 205:
  
 
# '''unshift function - '''which adds an''' element '''to an '''Array''' at the 1st position
 
# '''unshift function - '''which adds an''' element '''to an '''Array''' at the 1st position
# '''shift function''' - which removes the<br/> first''' element '''from an '''Array.'''
+
# '''shift function''' - which removes the first''' element '''from an '''Array.'''
 
+
 
+
  
 
|-
 
|-
Line 243: Line 235:
  
  
'''unshift '''function will insert''' '''an '''element '''at the''' '''first''' '''position  
+
'''unshift '''function will insert an '''element '''at the first position  
  
 
i.e before 1
 
i.e before 1
  
  
'''shift '''function will remove''' '''an''' element '''from the''' '''first position.  
+
'''shift '''function will remove an '''element '''from the first position.  
  
  
In our case,''' '''zero''' '''will be removed.
+
In our case, '''zero''' will be removed.
  
 
|-
 
|-
Line 266: Line 258:
 
# '''1st argument '''is the''' Array''' in which to add an '''element'''
 
# '''1st argument '''is the''' Array''' in which to add an '''element'''
 
# '''2nd argument '''is the '''element '''to be added into the '''Array'''
 
# '''2nd argument '''is the '''element '''to be added into the '''Array'''
 
  
  
Line 274: Line 265:
  
 
* This is the '''Array '''from which the '''element '''needs to be removed.
 
* This is the '''Array '''from which the '''element '''needs to be removed.
 
  
  
Line 281: Line 271:
 
| 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 '''first position '''of an array.
+
Both these functions works at '''first position '''of an '''Array'''.
 
+
 
+
  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''
| 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;"| We can collect the element removed by '''shift function''' into some variable.
+
| 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;"| We can collect the '''element''' removed by '''shift function''' into some variable.
  
  
Line 303: Line 291:
  
  
| 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;"| Then switch to the '''terminal '''and '''execute '''the '''Perl''' '''script.'''
+
| 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;"| Then switch to the '''terminal '''and execute the '''Perl''' script.  
  
  
Line 326: Line 314:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''slide'''
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''slide'''
| 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;"| * '''splice function '''removes an '''element''' from a specified position of an '''Array'''.
+
| 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;"|  
 +
* '''splice function '''removes an '''element''' from a specified position of an '''Array'''.
 
* The '''return value '''of this '''function '''is an '''Array '''of removed '''elements'''.
 
* The '''return value '''of this '''function '''is an '''Array '''of removed '''elements'''.
 
  
  
Line 336: Line 324:
  
  
Go to the '''perlArray dot pl''' file which we created earlier.
+
Go to the '''perlArray dot pl''' file, which we created earlier.
  
 
|-
 
|-
Line 348: Line 336:
 
'''print "Spliced Array: @splicedArray\n";'''
 
'''print "Spliced Array: @splicedArray\n";'''
 
| 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;"| Type the piece of code shown on the screen.
 
| 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;"| Type the piece of code shown on the screen.
 
  
 
We need to provide
 
We need to provide
Line 365: Line 352:
  
  
| 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;"| Then switch to the '''terminal '''and '''execute '''the '''Perl script''' by typing -''' '''
+
| 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;"| Then switch to the '''terminal '''and execute the '''Perl script''' by typing -
  
  
Line 386: Line 373:
 
* The return value of this '''function '''is an '''Array'''.
 
* The return value of this '''function '''is an '''Array'''.
 
* The '''elements '''of this '''Array '''are the divided portions of the '''string'''.
 
* The '''elements '''of this '''Array '''are the divided portions of the '''string'''.
 
  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide
| 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;"| * '''join function''' joins the '''elements '''of an '''Array, '''using the specified '''delimiter'''.
+
| 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;"|  
 +
* '''join function''' joins the '''elements '''of an '''Array, '''using the specified '''delimiter'''.
 
* It returns a string of joined '''elements.'''
 
* It returns a string of joined '''elements.'''
 
  
  
Line 406: Line 392:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"|  
| 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;"| Now let us understand all these functions using a sample program.
+
| 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;"| Now let us understand all these '''functions''' using a sample program.
  
 
|-
 
|-
Line 417: Line 403:
  
 
'''gedit arrayFunctions dot pl space ampersand'''
 
'''gedit arrayFunctions dot pl space ampersand'''
 
 
This will open '''arrayFunctions dot pl''' file in '''gedit'''.
 
 
 
 
  
 
|-
 
|-
Line 471: Line 451:
  
  
In this case, each word of the variable string will become an '''element '''of an '''Array.'''
+
In this case, each word of the '''variable string''' will become an '''element '''of an '''Array.'''
  
  
Line 484: Line 464:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''
| 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 us understand each 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;"| Let us understand each '''function'''.
  
 
|-
 
|-
Line 493: Line 473:
 
* '''2nd '''is the '''string '''which needs to be split.
 
* '''2nd '''is the '''string '''which needs to be split.
  
'''Delimiters''' can be specified in f'''orward slash, single or double quotes.'''
+
'''Delimiters''' can be specified in '''forward slash, single or double quotes.'''
  
 
|-
 
|-
Line 499: Line 479:
 
| 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;"| '''join function '''takes 2 '''arguments.'''
 
| 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;"| '''join function '''takes 2 '''arguments.'''
  
# '''1st '''is the '''delimiter '''by which the '''Array elements '''needs to be joined'''.'''
+
* '''1st '''is the '''delimiter '''by which the '''Array elements '''needs to be joined'''.'''
# '''2nd '''is the '''Array.'''
+
* '''2nd '''is the '''Array.'''
  
'''Delimiters''' can be specified in '''single or double'''
+
'''Delimiters''' can be specified in '''single or double quotes.'''
 
+
'''quotes.'''
+
  
 
|-
 
|-
Line 521: Line 499:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''terminal'''and type
 
+
  
 
'''perl arrayFunctions.pl'''
 
'''perl arrayFunctions.pl'''
  
 
+
| 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;"| Then switch to the '''terminal '''and execute the '''Perl script''' by typing -  
 
+
| 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;"| Then switch to the '''terminal '''and '''execute '''the '''Perl script''' by typing -  
+
  
  
Line 576: Line 551:
 
'''];'''
 
'''];'''
 
| 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;"| The following output is displayed on the '''terminal'''<nowiki>. <pause></nowiki>
 
| 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;"| The following output is displayed on the '''terminal'''<nowiki>. <pause></nowiki>
 
 
''<nowiki><< Will just highlight the output and will not read it while recording >></nowiki>''
 
 
 
  
  
Line 592: Line 562:
 
* basic functions which can be performed on '''Array'''
 
* basic functions which can be performed on '''Array'''
 
* using sample programs
 
* using sample programs
 
  
  
Line 599: Line 568:
 
| 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;"| Here is assignment for you -
 
| 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;"| Here is assignment for you -
  
# '[http://script.spoken-tutorial.org/index.php/Perl script.spoken-tutorial.org/index.php/Perl]' split the above '''string''' at '/' '''delimiter'''
+
# 'script.spoken-tutorial.org/index.php/Perl script.spoken-tutorial.org/index.php/Perl]'  
 +
# split the above '''string''' at '/' '''delimiter'''
 
# Add '''https:// '''at the start of an newly created '''Array'''
 
# Add '''https:// '''at the start of an newly created '''Array'''
# Remove '''element “Perl” '''from an '''Array'''
+
# Remove '''element “Perl” '''from the '''Array'''
 
# Declare number '''Array '''and sort it
 
# Declare number '''Array '''and sort it
 
 
  
 
|-
 
|-

Latest revision as of 13:01, 17 June 2014

Title Of Script: Array functions in Perl

Author: Amol Brahmankar

Keywords: Array functions in perl video tutorial.


Visual Cue
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;


whereas, 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

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.
  • 1st is the delimiter by which the Array elements needs to be joined.
  • 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 terminaland type

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>


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 script.spoken-tutorial.org/index.php/Perl]'
  2. split the above string at '/' delimiter
  3. Add https:// at the start of an newly created Array
  4. Remove element “Perl” from the Array
  5. 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