Difference between revisions of "PERL/C3/Referencing-and-Dereferencing/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| Border = 1 | <center>''' Time '''</center> | <center>'''Narration'''</center> |- | 00:01 | Welcome to the''' Spoken Tutorial''' on''' Referencing and Dereferencing in...")
 
Line 404: Line 404:
 
|  09:15
 
|  09:15
 
| '''print''' statement will print each element of the '''hash'''.
 
| '''print''' statement will print each element of the '''hash'''.
 
  
 
|-
 
|-
Line 446: Line 445:
 
|-
 
|-
 
|  10:14
 
|  10:14
| Here is an assignment for you.
+
| Here is an assignment for you. Add new keys “'''Saturday'''” and “'''Sunday'''” in hash '''weektemp''' in our''' hashRefadd dot pl file.'''
Add new keys “'''Saturday'''” and “'''Sunday'''” in hash '''weektemp''' in our''' hashRefadd dot pl file.'''
+
  
 
|-
 
|-

Revision as of 15:16, 23 November 2015

Time
Narration
00:01 Welcome to the Spoken Tutorial on Referencing and Dereferencing in Perl.
00:07 In this tutorial we will learn about
  • Scalar References
  • Array References
  • Hash References
  • Dereferences and
  • How to add, remove, access elements of array/hash references
00:22 For this tutorial, I am using
  • Ubuntu Linux 12.04 operating system
  • Perl 5.14.2
  • gedit Text Editor
00:33 You can use any text editor of your choice.
00:37 You should have working knowledge of
  • Perl programming
  • Array functions and
  • Hash functions.
00:43 If not, then go through the relevant Perl tutorials on this website.
00:49 What is References?
00:51 A reference is a pointer or an address to a variable, array, hash or a subroutine.
00:58 It does not contain data directly.
01:01 Reference is an easy, compact scalar value.
01:05 Reference will improve the performance of Perl code when you pass or return large data structures.
01:12 It saves memory as it passes a reference to a subroutine rather than passing a value.
01:18 Easy to manage complicated Perl data structures.
01:22 Let us learn how to create a reference.
01:25 We can create a reference for any variable, subroutine or value, by putting a backslash (\) in front of it.
01:33 A scalar variable is referenced by backslash and dollar sign ($) as shown here.
01:39 An array variable is referenced by backslash and at the rate(@) symbol.
01:45 A hash variable is referenced by backslash and percentage(%) symbol as shown in the example here.
01:53 what is dereference?
01:55 When a reference is dereferenced, the actual value is returned.
02:00 Dereference is done by enclosing the reference variable within curly brackets.
02:06 And preceding the left curly bracket with a character denoting the type of reference it is.
02:12 Let us see how to dereference variables.
02:16 A scalar variable is dereferenced by dollar sign ($) and curly brackets.
02:21 An array variable is dereferenced by at the rate (@) symbol and curly brackets.
02:27 A hash variable is dereferenced by percentage(%) symbol and curly brackets.
02:33 Let us see a simple program for Scalar reference and dereference.
02:38 Let me open a sample program in gedit Text editor.
02:43 Open the terminal and type gedit scalarRef dot pl ampersand and press Enter.
02:50 Type the following code as displayed on the screen
02:55 Let me explain the code.
02:57 First line declares a scalar variable '$a' and initialized to 10.
03:03 As mentioned earlier, a scalar variable is referenced by backslash and dollar sign ($)
03:10 This line will print memory address of the variable that is created as reference.
03:16 To print the actual value, the variable is dereferenced by curly brackets preceded by $.
03:23 Hereref() function will return the reference type such as scalar or array or hash.
03:30 Now, press Ctrl+S to save the file.
03:34 Let us execute the program.
03:36 Switch to the terminal and type perl scalarRef dot pl and press Enter.
03:43 The output is displayed as shown.
03:46 First line shows the memory address where the value 10 is stored.
03:51 The second line returns the actual value 10.
03:55 Ref() function returns 'Scalar' as output.
03:59 Next, let us understand how to create a reference and dereference array by using a sample program.
04:07 I already have a sample program. Let me open it in gedit Text editor.
04:13 In the terminal, type gedit arrayRef dot pl ampersand and press Enter.
04:20 Type the following code as displayed on the screen in the arrayRef dot pl file.
04:26 Let me explain the code now.
04:28 Here, in the first line, I have declared an array @color and initialised it with three values.
04:35 It is referenced with backslash @color which is the array name and assigned to $colorRef.
04:42 The print statement will print the reference value and the dereferenced value.
04:47 Now, press Ctrl+S to save the file.
04:51 Let us execute the program.
04:53 Switch back to the terminal and type perl arrayRef dot pl and press Enter.
05:00 The output is displayed as shown here
05:04 The first line shows the output of the memory address of the variable that is created as reference.
05:10 The second line shows the actual value that is dereferenced.
05:16 Next, we will see how to declare direct reference for an array.
05:21 Let’s come back to our program.
05:24 I have changed the existing program to show the direct reference for an array.
05:29 You can create a direct reference for an array by using square brackets [] as shown.
05:35 Use arrow operator (->) to dereference.
05:39 print statement will print 'Green' as output.
05:43 Here the print statement takes the value of index[1]. i.e Green in our program.
05:50 Press Ctrl+S to save the file.
05:54 Switch back to the terminal and type perl arrayRef dot pl and press Enter to execute.
06:03 I’ll show an example on how to use the direct hash reference in the same code file.

So, let’s switch to gedit.

06:11 You can create a direct reference to hash by using curly brackets {} as shown here.
06:18 Use arrow operator (->) to dereference it. “Name” is the hash key.
06:24 On executing this block of code, both the print statements will print 'Sunil' as output.
06:31 Next we will see how to add, remove, access elements to array reference with a sample program.
06:39 I already have a sample program. Let me open it in gedit Text editor
06:45 Open the terminal and type gedit arrayRefadd dot pl ampersand and press Enter
06:54 arrayRefadd.pl file is now open in gedit. Type the code as shown here in your file.
07:02 The first line initializes an array.
07:06 We have referenced an array with backslash @numarray and assigned to $ref.
07:13 Now, we will see how to access a particular element from the array reference.
07:19 We need to use the array index in square brackets “[ ]” to access the particular value. And an arrow operator (“->”) to dereference it.
07:28 The print statement will print the value of index[0]
07:32 Push function adds elements at the last position of an array reference.

In our case 5,6,7 is added to at the end of the existing array 1,2,3,4.

07:47 This print statement shows the output after adding to the array reference.
07:53 Pop function removes an element from the last position of an array reference
07:58 In our example, 7 will be removed from the existing array reference.
08:03 print statement shows the output after deleting from the array reference.
08:08 Now, press Ctrl+S to save the file.
08:11 Let us execute the program.
08:14 Switch back to the terminal and type perl arrayRefadd dot pl and press Enter.
08:22 The output is displayed as shown here
08:26 Now let us see another sample program to add, remove, and access elements of hash reference.
08:34 In the terminal type gedit hashRefadd dot pl ampersand and press Enter.
08:42 This will open the file hashRefadd.pl in gedit.
08:47 Let me explain the sample program.
08:50 I have declared a direct hash reference that can be stored in a scalar variable $weektemp.
08:57 I have used curly brackets to represent the hash reference and the arrow operator to dereference.
09:04 This code stores the temperature values from Monday to Friday.
09:09 I am using the “keys” built-in function to loop through the keys of the hash.
09:15 print statement will print each element of the hash.
09:19 We can access the particular value of an element as shown here.
09:25 print statement will print the temperature on Monday.
09:29 Now, save the file.
09:32 Switch to terminal and type perl hashRefadd dot pl and press Enter to see the output.
09:41 The hash keys and hash values are stored in a random order.
09:46 The displayed output is not related to the order in which they were added.
09:52 With this, we come to the end of this tutorial. Let us summarize.
09:57 In this tutorial, we learnt about:
  • Scalar References
  • Array References
  • Hash References
  • Dereferences and
  • How to add, remove, access elements of array/hash references with examples.
10:14 Here is an assignment for you. Add new keys “Saturday” and “Sunday” in hash weektemp in our hashRefadd dot pl file.
10:24 Delete key “Saturday” at the end.
10:27 Print hash weektemp.
10:30 Save and execute the program. Now check the result.
10:35 The video at the following link summarises the Spoken Tutorial project.

Please download and watch it

10:42 We conduct workshops and give certificates for those who pass our online tests.

For more details, please write to us.

10:51 Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

11:02 This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya, Sandhya.np14