Difference between revisions of "PERL/C3/Referencing-and-Dereferencing/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 10: | Line 10: | ||
|- | |- | ||
| 00:07 | | 00:07 | ||
− | | In this tutorial we will learn about | + | | 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 | | 00:22 | ||
− | | For this tutorial, I am using | + | | For this tutorial, I am using: |
− | + | '''Ubuntu Linux 12.04''' operating system | |
− | + | '''Perl 5.14.2''' | |
− | + | '''gedit Text Editor''' | |
|- | |- | ||
| 00:33 | | 00:33 | ||
− | |You can use any text editor of your choice. | + | |You can use any text-editor of your choice. |
|- | |- | ||
| 00:37 | | 00:37 | ||
− | | You should have working knowledge of | + | | You should have working knowledge of: |
− | + | '''Perl''' programming | |
− | + | '''Array functions''' and | |
− | + | '''Hash functions'''. | |
|- | |- | ||
Line 41: | Line 41: | ||
|- | |- | ||
| 00:49 | | 00:49 | ||
− | | What is''' | + | | What is a''' Reference'''? |
|- | |- | ||
| 00:51 | | 00:51 | ||
− | |A''' reference''' is a '''pointer''' or an '''address''' to ''' | + | |A''' reference''' is a '''pointer''' or an '''address''' to a '''variable, array, hash''' or a '''subroutine'''. |
|- | |- | ||
Line 57: | Line 57: | ||
|- | |- | ||
| 01:05 | | 01:05 | ||
− | | '''Reference''' will improve the performance of '''Perl''' code when you pass or return large data structures. | + | | '''Reference''' will improve the performance of '''Perl''' code when you pass or return large data-structures. |
|- | |- | ||
Line 73: | Line 73: | ||
|- | |- | ||
| 01:25 | | 01:25 | ||
− | | We can create a''' reference''' for any '''variable, subroutine''' or value, by putting a '''backslash (\)''' in front of it. | + | | We can create a''' reference''' for any '''variable, subroutine''' or '''value''', by putting a '''backslash (\)''' in front of it. |
|- | |- | ||
| 01:33 | | 01:33 | ||
− | |A''' scalar variable''' is''' | + | |A''' scalar variable''' is''' reference'''d by backslash and dollar sign ($) as shown here. |
|- | |- | ||
| 01:39 | | 01:39 | ||
− | |An''' array variable''' is''' | + | |An''' array variable''' is''' reference'''d by backslash and '''at the rate(@)''' symbol. |
|- | |- | ||
| 01:45 | | 01:45 | ||
− | |A''' hash variable''' is''' | + | |A''' hash variable''' is''' reference'''d by backslash and '''percentage(%)''' symbol as shown in the example here. |
|- | |- | ||
| 01:53 | | 01:53 | ||
− | | | + | | what is '''dereference'''? |
|- | |- | ||
| 01:55 | | 01:55 | ||
− | |When a '''reference''' is''' | + | |When a '''reference''' is''' dereference'''d, the actual value is returned. |
|- | |- | ||
| 02:00 | | 02:00 | ||
− | |'''Dereference''' is done by enclosing the''' reference''' variable within curly brackets | + | |'''Dereference''' is done by enclosing the''' reference''' variable within curly brackets |
|- | |- | ||
| 02:06 | | 02:06 | ||
− | | | + | |and preceding the left curly bracket with a character denoting the type of''' reference''' it is. |
|- | |- | ||
Line 109: | Line 109: | ||
|- | |- | ||
| 02:16 | | 02:16 | ||
− | | A''' scalar | + | | A''' scalar''' variable is dereferenced by dollar sign ($) and curly brackets. |
|- | |- | ||
| 02:21 | | 02:21 | ||
− | |An''' array | + | |An''' array''' variable is dereferenced by '''at the rate (@)''' symbol and curly brackets. |
|- | |- | ||
Line 125: | Line 125: | ||
|- | |- | ||
| 02:38 | | 02:38 | ||
− | | Let me open a sample program in''' gedit Text editor.''' | + | | Let me open a sample program in ''''gedit' Text editor.''' |
|- | |- | ||
| 02:43 | | 02:43 | ||
− | | Open the '''terminal''' and type''' gedit scalarRef dot pl ampersand''' and press '''Enter'''. | + | | Open the '''terminal''' and type: '''gedit scalarRef dot pl ampersand''' and press '''Enter'''. |
|- | |- | ||
| 02:50 | | 02:50 | ||
− | | Type the following code as displayed on the screen | + | | Type the following code as displayed on the screen. |
|- | |- | ||
Line 145: | Line 145: | ||
|- | |- | ||
| 03:03 | | 03:03 | ||
− | |As mentioned earlier, a | + | |As mentioned earlier, a scalar variable is''' reference'''d by backslash and dollar sign ($). |
|- | |- | ||
| 03:10 | | 03:10 | ||
− | |This line will print '''memory address''' of the | + | |This line will print '''memory address''' of the variable that is created as''' reference'''. |
|- | |- | ||
| 03:16 | | 03:16 | ||
− | |To print the actual value, the '''variable''' is''' | + | |To print the actual value, the '''variable''' is''' dereference'''d by curly brackets preceded by '$'. |
|- | |- | ||
| 03:23 | | 03:23 | ||
− | |Here'''ref()''' function will return the''' reference''' type such as '''scalar''' or '''array''' or '''hash'''. | + | |Here, '''ref()''' function will return the''' reference''' type such as '''scalar''' or '''array''' or '''hash'''. |
|- | |- | ||
Line 169: | Line 169: | ||
|- | |- | ||
| 03:36 | | 03:36 | ||
− | | Switch to the '''terminal''' and type '''perl scalarRef dot pl''' and press''' Enter'''. | + | | Switch to the '''terminal''' and type: '''perl scalarRef dot pl''' and press''' Enter'''. |
|- | |- | ||
Line 177: | Line 177: | ||
|- | |- | ||
| 03:46 | | 03:46 | ||
− | |First line shows the memory address where the value 10 is stored. | + | |First line shows the '''memory address''' where the value 10 is stored. |
|- | |- | ||
Line 185: | Line 185: | ||
|- | |- | ||
| 03:55 | | 03:55 | ||
− | |'''Ref() | + | |'''Ref()''' function returns "SCALAR" as output. |
|- | |- | ||
Line 193: | Line 193: | ||
|- | |- | ||
| 04:07 | | 04:07 | ||
− | | I already have a sample program. Let me open it in'' | + | | I already have a sample program. Let me open it in 'gedit' Text editor. |
|- | |- | ||
| 04:13 | | 04:13 | ||
− | | In the '''terminal''', type''' gedit arrayRef dot pl ampersand''' and press '''Enter'''. | + | | In the '''terminal''', type: '''gedit arrayRef dot pl ampersand''' and press '''Enter'''. |
|- | |- | ||
| 04:20 | | 04:20 | ||
− | | Type the following code as displayed on the screen in the '''arrayRef dot pl''' file. | + | | Type the following code as displayed on the screen, in the '''arrayRef dot pl''' file. |
|- | |- | ||
Line 209: | Line 209: | ||
|- | |- | ||
| 04:28 | | 04:28 | ||
− | |Here, in the first line, I have declared an ''' | + | |Here, in the first line, I have declared an array '''@color''' and initialized it with three values. |
|- | |- | ||
| 04:35 | | 04:35 | ||
− | |It is''' | + | |It is '''reference'''d with '''backslash @color''' which is the array name and assigned to''' $colorRef.''' |
|- | |- | ||
| 04:42 | | 04:42 | ||
− | |The '''print''' statement will print the''' reference''' value and the''' | + | |The '''print''' statement will print the''' reference''' value and the''' dereference'''d value. |
|- | |- | ||
Line 229: | Line 229: | ||
|- | |- | ||
| 04:53 | | 04:53 | ||
− | | Switch back to the '''terminal''' and type '''perl arrayRef dot pl''' and press''' Enter'''. | + | | Switch back to the '''terminal''' and type: '''perl arrayRef dot pl''' and press''' Enter'''. |
|- | |- | ||
| 05:00 | | 05:00 | ||
− | | The output is displayed as shown here | + | | The output is displayed as shown here. |
|- | |- | ||
Line 241: | Line 241: | ||
|- | |- | ||
| 05:10 | | 05:10 | ||
− | |The second line shows the actual value that is''' | + | |The second line shows the actual value that is''' dereference'''d. |
|- | |- | ||
Line 253: | Line 253: | ||
|- | |- | ||
| 05:24 | | 05:24 | ||
− | |I have changed the existing program to show the '''direct reference''' for an | + | |I have changed the existing program to show the '''direct reference''' for an array. |
|- | |- | ||
| 05:29 | | 05:29 | ||
− | | You can create a '''direct reference''' for an | + | | You can create a '''direct reference''' for an array by using square brackets<nowiki> [] </nowiki>as shown. |
|- | |- | ||
Line 265: | Line 265: | ||
|- | |- | ||
| 05:39 | | 05:39 | ||
− | |'''print''' statement will print | + | |'''print''' statement will print "Green" as output. |
|- | |- | ||
| 05:43 | | 05:43 | ||
− | |Here the '''print''' statement takes the value of index<nowiki>[1]. </nowiki>i.e | + | |Here, the '''print''' statement takes the value of index<nowiki>[1]. </nowiki> i.e 'Green' in our program. |
|- | |- | ||
Line 277: | Line 277: | ||
|- | |- | ||
| 05:54 | | 05:54 | ||
− | | Switch back to the '''terminal''' and type '''perl arrayRef dot pl''' and press''' Enter''' to execute. | + | | Switch back to the '''terminal''' and type: '''perl arrayRef dot pl''' and press''' Enter''' to execute. |
|- | |- | ||
| 06:03 | | 06:03 | ||
− | | I’ll show an example on how to use the direct hash reference in the same code file. | + | | I’ll show an example on how to use the '''direct hash reference''' in the same code file. |
So, let’s switch to '''gedit'''. | So, let’s switch to '''gedit'''. | ||
Line 290: | Line 290: | ||
|- | |- | ||
| 06:18 | | 06:18 | ||
− | | Use '''arrow operator (->)''' to dereference it. | + | | Use '''arrow operator (->)''' to dereference it. “Name” is the '''hash key'''. |
|- | |- | ||
| 06:24 | | 06:24 | ||
− | | On executing this block of code, both the '''print''' statements will print | + | | On executing this block of code, both the '''print''' statements will print "Sunil" as output. |
|- | |- | ||
Line 302: | Line 302: | ||
|- | |- | ||
| 06:39 | | 06:39 | ||
− | | I already have a sample program. Let me open it in''' gedit | + | | I already have a sample program. Let me open it in''' gedit''' text-editor. |
|- | |- | ||
| 06:45 | | 06:45 | ||
− | | Open the''' terminal''' and type''' gedit arrayRefadd dot pl ampersand '''and press''' Enter''' | + | | Open the''' terminal''' and type:''' gedit arrayRefadd dot pl ampersand '''and press''' Enter'''. |
|- | |- | ||
| 06:54 | | 06:54 | ||
− | | | + | | 'arrayRefadd.pl' file is now open in''' gedit.''' Type the code as shown here, in your file. |
|- | |- | ||
Line 318: | Line 318: | ||
|- | |- | ||
| 07:06 | | 07:06 | ||
− | | We have ''' | + | | We have '''reference'''d an array with '''backslash @numarray''' and assigned to''' $ref.''' |
|- | |- | ||
Line 326: | Line 326: | ||
|- | |- | ||
| 07:19 | | 07:19 | ||
− | | We need to use the '''array index''' in square brackets<nowiki> “[ ]” </nowiki>to access the particular value | + | | We need to use the '''array index''' in square brackets<nowiki> “[ ]” </nowiki> to access the particular value and an '''arrow operator (“->”)''' to''' dereference''' it. |
|- | |- | ||
| 07:28 | | 07:28 | ||
− | | The '''print''' statement will print the value of index<nowiki>[0]</nowiki> | + | | The '''print''' statement will print the value of index <nowiki>[0]</nowiki>. |
|- | |- | ||
| 07:32 | | 07:32 | ||
− | | ''' | + | | '''push()''' function adds '''elements''' at the last position of an''' array reference.''' |
− | In our case 5,6,7 | + | In our case, 5, 6, 7 are added to the end of the existing array 1, 2, 3, 4. |
|- | |- | ||
| 07:47 | | 07:47 | ||
− | | This '''print''' statement shows the output after adding to the''' array reference.''' | + | | This '''print''' statement shows the output, after adding to the''' array reference.''' |
|- | |- | ||
| 07:53 | | 07:53 | ||
− | | ''' | + | | '''pop()''' function removes an '''element''' from the last position of an''' array reference'''. |
|- | |- | ||
Line 363: | Line 363: | ||
|- | |- | ||
| 08:14 | | 08:14 | ||
− | | Switch back to the '''terminal''' and type '''perl arrayRefadd dot pl''' and press''' Enter'''. | + | | Switch back to the '''terminal''' and type: '''perl arrayRefadd dot pl''' and press''' Enter'''. |
|- | |- | ||
| 08:22 | | 08:22 | ||
− | | The output is displayed as shown here | + | | The output is displayed as shown here. |
|- | |- | ||
| 08:26 | | 08:26 | ||
− | | Now let us see another sample program to add, remove, and access '''elements''' of''' hash reference.''' | + | | Now, let us see another sample program to add, remove, and access '''elements''' of''' hash reference.''' |
|- | |- | ||
| 08:34 | | 08:34 | ||
− | | | + | | On the '''terminal''', type: '''gedit hashRefadd dot pl ampersand''' and press '''Enter'''. |
|- | |- | ||
| 08:42 | | 08:42 | ||
− | | This will open the file | + | | This will open the file 'hashRefadd.pl' in''' gedit'''. |
|- | |- | ||
Line 399: | Line 399: | ||
|- | |- | ||
| 09:09 | | 09:09 | ||
− | | I am using the '''“keys” built-in function''' to loop through the | + | | I am using the '''“keys” built-in function''' to loop through the keys of the hash. |
|- | |- | ||
| 09:15 | | 09:15 | ||
− | | '''print''' statement will print each element of the | + | | '''print''' statement will print each element of the hash. |
|- | |- | ||
Line 419: | Line 419: | ||
|- | |- | ||
| 09:32 | | 09:32 | ||
− | | Switch to '''terminal''' and type '''perl hashRefadd dot pl''' and press''' Enter '''to see the output. | + | | Switch to '''terminal''' and type: '''perl hashRefadd dot pl''' and press''' Enter '''to see the output. |
|- | |- | ||
Line 436: | Line 436: | ||
|09:57 | |09:57 | ||
| In this tutorial, we learnt about: | | 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 | | 10:14 | ||
− | | Here is an assignment for you. Add new keys | + | | Here is an assignment for you. Add new keys “Saturday” and “Sunday” in hash '''weektemp''', in our''' hashRefadd dot pl file.''' |
|- | |- | ||
| 10:24 | | 10:24 | ||
− | | Delete key | + | | Delete key “Saturday” at the end. |
|- | |- | ||
Line 457: | Line 456: | ||
|- | |- | ||
| 10:30 | | 10:30 | ||
− | | Save and execute the program. Now check the result. | + | | '''Save''' and '''execute''' the program. Now check the result. |
|- | |- | ||
| 10:35 | | 10:35 | ||
− | | The video at the following link | + | | The video at the following link summarizes the '''Spoken Tutorial''' project.Please download and watch it. |
− | Please download and watch it | + | |
|- | |- | ||
| 10:42 | | 10:42 | ||
− | | We conduct workshops and give certificates for those who pass our online tests. | + | | We conduct workshops and give certificates for those who pass our online tests. For more details, please write to us. |
− | For more details, please write to us. | + | |
|- | |- | ||
Line 476: | Line 473: | ||
|- | |- | ||
| 11:02 | | 11:02 | ||
− | | This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching. | + | | This is Nirmala Venkat from '''IIT Bombay''', signing off. Thanks for watching. |
|} | |} |
Latest revision as of 18:13, 20 February 2017
|
|
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 a Reference? |
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 | Here, ref() 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 initialized 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 are added to 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 | On 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 summarizes 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. |