Difference between revisions of "Linux-AWK/C2/Basics-of-Single-Dimensional-Array-in-awk/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 this spoken tutorial on '''Basics of single dimensional array''' in '''aw...")
 
Line 5: Line 5:
 
|-
 
|-
 
| 00:01
 
| 00:01
| Welcome to this spoken tutorial on '''Basics of single dimensional array''' in '''awk'''.
+
| Welcome to this '''spoken tutorial''' on '''Basics of single dimensional array in awk'''.
  
 
|-
 
|-
 
| 00:07
 
| 00:07
| In this tutorial we will learn about-  '''Arrays''' in '''awk'''
+
| In this tutorial, we will learn about-  '''Arrays''' in '''awk''',
  
 
|-
 
|-
 
|00:12
 
|00:12
Assigning '''array elements'''
+
assigning '''array elements''',
  
 
|-
 
|-
 
|00:15
 
|00:15
How it is different from '''arrays''' in other programming languages and  Refer the '''elements '''of an '''array '''
+
how it is different from '''arrays''' in other programming languages and   
 +
refer the '''elements '''of an '''array '''.
  
 
|-
 
|-
Line 25: Line 26:
 
|-
 
|-
 
|00:26
 
|00:26
| To record this tutorial, I am using  '''Ubuntu Linux 16.04 Operating System '''and '''gedit text editor 3.20.1'''
+
| To record this tutorial, I am using:  
 +
'''Ubuntu Linux 16.04 Operating System '''and
 +
'''gedit text editor 3.20.1'''
  
 
|-
 
|-
 
|00:38
 
|00:38
| You can use any text editor of your choice.
+
| You can use any '''text editor''' of your choice.
  
 
|-
 
|-
Line 53: Line 56:
 
| What is an '''array''' in '''awk'''?  
 
| What is an '''array''' in '''awk'''?  
  
'''awk''' supports '''arrays''' for storing related '''elements'''.
+
'''awk''' supports '''arrays''' for storing related '''element'''s.
  
 
|-
 
|-
 
|01:18
 
|01:18
|  '''Elements '''can be a '''number '''or a '''string'''.
+
|  '''Element'''s can be a number or a '''string'''.
  
 
|-
 
|-
Line 65: Line 68:
 
|-
 
|-
 
|01:24
 
|01:24
|  This means that each '''array element '''is an '''index-value pair.'''
+
|  This means that each '''array element '''is an '''index-value '''pair.
  
 
|-
 
|-
Line 81: Line 84:
 
|-
 
|-
 
|01:41
 
|01:41
|  Also there is no need to specify how many '''elements''' the '''array '''will contain.
+
|  Also, there is no need to specify how many '''elements''' the '''array '''will contain.
  
 
|-
 
|-
Line 93: Line 96:
 
|-
 
|-
 
|01:58
 
|01:58
| But in '''awk''', the '''index '''can be anything – any '''number '''or a '''string.'''
+
| But in '''awk''', the '''index '''can be anything – any number or a '''string.'''
  
 
|-
 
|-
Line 115: Line 118:
 
|-
 
|-
 
| 02:27
 
| 02:27
| I have already written the code and saved it as '''array_intro.awk'''
+
| I have already written the code and saved it as '''array_intro.awk'''.
  
 
|-
 
|-
Line 125: Line 128:
 
|-
 
|-
 
| 02:41
 
| 02:41
| Here I have taken weekdays as an example and written it inside the '''BEGIN section'''.
+
| Here, I have taken weekdays as an example and written it inside the '''BEGIN '''section.
  
 
|-
 
|-
Line 133: Line 136:
 
|-
 
|-
 
|02:52
 
|02:52
| I have set the '''index '''as 1 and the value as '''Sunday'''.
+
| I have set the '''index '''as 1 and the value as '''"Sunday"'''.
  
 
|-
 
|-
Line 139: Line 142:
 
| In this '''array element''', I have used a '''string '''as the '''index.'''
 
| In this '''array element''', I have used a '''string '''as the '''index.'''
  
So for '''index first, '''the value is '''Sunday'''.
+
So, for '''index "first", '''the value is '''"Sunday"'''.
  
 
|-
 
|-
Line 161: Line 164:
 
|-
 
|-
 
| 03:29
 
| 03:29
| Let me add day 6 in the '''array'''.
+
| Let me add '''day 6''' in the '''array'''.
  
 
|-
 
|-
Line 167: Line 170:
 
| Place the cursor at the end of the last line and press '''Enter'''.  
 
| Place the cursor at the end of the last line and press '''Enter'''.  
  
Then type the following
+
Then type the following.
  
 
|-
 
|-
 
| 03:42
 
| 03:42
| Save the file.
+
| '''Save''' the file.
  
 
|-
 
|-
Line 181: Line 184:
 
|-
 
|-
 
|03:49
 
|03:49
| Write the '''arrayname '''and the''' index '''within''' square brackets '''to refer an '''element '''at a particular '''index.'''
+
| Write the '''arrayname '''and the''' index '''within square brackets to refer an '''element, '''at a particular '''index.'''
  
 
Let us try this.
 
Let us try this.
Line 187: Line 190:
 
|-
 
|-
 
| 03:58
 
| 03:58
| Switch to the code once again.
+
| Switch to the '''code''' once again.
  
 
|-
 
|-
 
|04:01
 
|04:01
| Place the cursor in front of the closing '''curly brace'''.
+
| Place the cursor in front of the closing curly brace.
  
 
|-
 
|-
 
|04:05
 
|04:05
| Press '''Enter''' and type '''print '''''space '''''day '''''within square brackets '''''6'''
+
| Press '''Enter''' and type: '''print''' ''space'' '''day''' ''within square brackets'' '''6'''.
  
 
|-
 
|-
 
| 04:13
 
| 04:13
| Save the code.
+
| '''Save''' the '''code'''.
  
 
|-
 
|-
Line 207: Line 210:
 
|-
 
|-
 
| 04:20
 
| 04:20
| Go to the folder in which you downloaded and extracted the '''Code Files''' using '''cd command'''
+
| Go to the folder in which you downloaded and extracted the '''Code Files''' using ''''cd' command'''.
  
 
|-
 
|-
 
| 04:27
 
| 04:27
| Now type '''awk space hyphen small f space array_intro.awk '''
+
| Now, type: '''awk space hyphen small f space array_intro.awk '''.
  
Press '''Enter'''
+
Press '''Enter'''.
  
 
|-
 
|-
Line 229: Line 232:
 
|-
 
|-
 
| 04:55
 
| 04:55
| Switch to the code in the editor window.
+
| Switch to the '''code''' in the '''editor window'''.
  
 
|-
 
|-
Line 243: Line 246:
 
|-
 
|-
 
| 05:11
 
| 05:11
| Now I have added two '''if conditions.'''
+
| Now I have added two ''''if' conditions.'''
  
 
|-
 
|-
 
|05:15
 
|05:15
| The first '''if condition '''checks whether the '''index two '''is present in '''day.'''
+
| The first '''if '''condition checks whether the '''index two '''is present in '''day.'''
  
 
|-
 
|-
 
|05:21
 
|05:21
| If yes, then the corresponding '''print statement '''will get executed.
+
| If yes, then the corresponding '''print statement''' will get executed.
  
 
|-
 
|-
Line 263: Line 266:
 
| As we can see, '''index two''' is in the '''array''' and '''seven''' is not.
 
| As we can see, '''index two''' is in the '''array''' and '''seven''' is not.
  
Let’s execute this file to verify the output.
+
Let’s '''execute''' this file to verify the output.
  
 
|-
 
|-
 
| 05:44
 
| 05:44
| Switch to the '''terminal'''. Press the '''Up arrow key''' to get back the previously executed command.
+
| Switch to the '''terminal'''. Press the '''up arrow key''' to get back the previously executed '''command'''.
  
 
|-
 
|-
 
|05:51
 
|05:51
| Press '''Enter '''to execute.
+
| Press '''Enter '''to '''execute'''.
  
 
|-
 
|-
Line 281: Line 284:
 
| We will now make some more changes to the code.
 
| We will now make some more changes to the code.
  
Update the code as shown here.
+
'''Update''' the code as shown here.
  
 
|-
 
|-
Line 289: Line 292:
 
|-
 
|-
 
|06:09
 
|06:09
| This will check whether the value of''' index seven''' is null or not.
+
| This will check whether the value of''' index seven''' is '''null''' or not.
  
 
|-
 
|-
 
|06:14
 
|06:14
| If '''true''', it will print '''Index 7 is not null'''
+
| If '''true''', it will print '''Index 7 is not null'''.
  
 
|-
 
|-
 
|06:18
 
|06:18
| We already know that we don’t have any '''index '''with '''7''', so it will not print anything.
+
| We already know that we don’t have any '''index '''with '''7'''. So, it will not print anything.
  
 
|-
 
|-
 
| 06:24
 
| 06:24
| Next, we have change the '''print statement '''of the '''condition 7 '''in '''day.'''
+
| Next, we have changed the '''print statement '''of the '''condition 7 '''in '''day.'''
  
 
|-
 
|-
Line 313: Line 316:
 
| Switch to '''terminal.'''
 
| Switch to '''terminal.'''
  
Press the up arrow key to get the previously executed command.
+
Press the '''up arrow key''' to get the previously executed command.
  
 
|-
 
|-
Line 331: Line 334:
 
|-
 
|-
 
| 06:57
 
| 06:57
| When we write, '''<nowiki>day[7] </nowiki>'''''not equal to''''' null''', we are trying to access the '''element''' at '''index 7.'''
+
| When we write, '''<nowiki>day[7] </nowiki>''' ''not equal to'' ''' null''', we are trying to access the '''element''' at '''index 7.'''
  
 
|-
 
|-
Line 347: Line 350:
 
|-
 
|-
 
| 07:26
 
| 07:26
| So, remember this.
+
| So, remember this:
  
'''day at index 7 '''''not equal to '''''null '''is a wrong way to check the presence of an '''element'''.
+
'''day at index 7''' ''not equal to '' '''null '''is a wrong way to check the presence of an '''element'''.
  
 
|-
 
|-
Line 367: Line 370:
 
|-
 
|-
 
| 07:50
 
| 07:50
| In this tutorial we learnt about- '''Arrays''' in '''awk'''
+
| In this tutorial, we learnt about-  
 +
'''Arrays''' in '''awk''',
  
 
|-
 
|-
 
|07:54
 
|07:54
|  Assigning '''array elements'''
+
|  Assigning '''array elements''',
  
 
|-
 
|-
 
|07:56
 
|07:56
|  How it is different from '''arrays''' in other programming languages
+
|  How it is different from '''arrays''' in other programming languages,
  
 
|-
 
|-
 
|08:00
 
|08:00
|  Refer the '''elements '''of an '''array '''
+
|  Refer the '''elements '''of an '''array '''.
  
 
|-
 
|-
 
|08:03
 
|08:03
| As an assignment- Define an '''array flowerColor'''
+
| As an assignment-  
 +
Define an '''array flowerColor'''.
  
 
|-
 
|-
 
|08:07
 
|08:07
|  Index will be the names of the flowers
+
'''Index''' will be the names of the flowers.
  
 
|-
 
|-
 
|08:10
 
|08:10
|  Value will be the corresponding color of the flowers
+
'''Value''' will be the corresponding color of the flowers.
  
 
|-
 
|-
 
| 08:14
 
| 08:14
|  Insert entries for any five flowers of your choice
+
|  Insert entries for any five flowers of your choice.
  
 
|-
 
|-
 
|08:18
 
|08:18
|  Print the color of the fourth flower
+
'''Print''' the color of the fourth flower.
  
Check if the flower “Lotus” is present in the '''array'''
+
Check if the flower “Lotus” is present in the '''array'''.
  
 
|-
 
|-
 
| 08:25
 
| 08:25
| The video at the following link summarises the Spoken Tutorial project.
+
| The video at the following link summarises the '''Spoken Tutorial project'''.
  
 
Please download and watch it.
 
Please download and watch it.
Line 413: Line 418:
 
| The '''Spoken Tutorial Project''' team conducts workshops using spoken tutorials.  
 
| The '''Spoken Tutorial Project''' team conducts workshops using spoken tutorials.  
  
And gives certificates on passing online tests.
+
And, gives certificates on passing online tests.
  
 
|-
 
|-
Line 425: Line 430:
 
|-
 
|-
 
| 08:50
 
| 08:50
| Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
+
| Spoken Tutorial Project is funded by '''NMEICT, MHRD''', Government of India.
  
 
More information on this mission is available at this link.
 
More information on this mission is available at this link.
Line 431: Line 436:
 
|-
 
|-
 
| 09:01
 
| 09:01
| The script has been contributed by Antara. And this is Praveen from IIT Bombay signing off.
+
| The script has been contributed by Antara. And, this is Praveen from '''IIT Bombay''', signing off.
  
 
Thanks for joining
 
Thanks for joining
  
 
|}
 
|}

Revision as of 17:13, 10 July 2019

Time
Narration
00:01 Welcome to this spoken tutorial on Basics of single dimensional array in awk.
00:07 In this tutorial, we will learn about- Arrays in awk,
00:12 assigning array elements,
00:15 how it is different from arrays in other programming languages and

refer the elements of an array .

00:23 We will do this through some examples.
00:26 To record this tutorial, I am using:

Ubuntu Linux 16.04 Operating System and gedit text editor 3.20.1

00:38 You can use any text editor of your choice.
00:42 To practice this tutorial, you should have gone through previous awk tutorials on our website.
00:49 You should have some basic knowledge of any programming language like C or C++.
00:56 If not, then please go through the corresponding tutorials on our website.
01:02 The files used in this tutorial are available in the Code Files link on this tutorial page.

Please download and extract them.

01:11 What is an array in awk?

awk supports arrays for storing related elements.

01:18 Elements can be a number or a string.
01:21 Arrays in awk are associative.
01:24 This means that each array element is an index-value pair.
01:29 It looks very similar to arrays in any other programming language.
01:33 But there are some important differences.
01:36 First, we do not need to declare an array before using it.
01:41 Also, there is no need to specify how many elements the array will contain.
01:47 In programming languages, array index is generally a positive integer.
01:52 Usually the index starts from 0, then 1, then 2 and so on.
01:58 But in awk, the index can be anything – any number or a string.
02:03 This is the syntax of assigning an array element in awk.

Array name can be any valid variable name.

02:11 Here the index can be an integer or a string.
02:16 Strings have to be written inside double quotes, whether it is index name or a value.
02:23 Let’s understand this with an example.
02:27 I have already written the code and saved it as array_intro.awk.
02:34 This file is available in the Code Files link below the player.

Please download and use it.

02:41 Here, I have taken weekdays as an example and written it inside the BEGIN section.
02:48 Here, the name of the array is day.
02:52 I have set the index as 1 and the value as "Sunday".
02:57 In this array element, I have used a string as the index.

So, for index "first", the value is "Sunday".

03:06 The entire array is constructed likewise.
03:10 Notice here, the array elements are not in a sequence.

I have declared day four before day three.

03:18 In awk arrays, index need not to be in a sequential manner.
03:23 Advantage of associative array is that new pairs can be added at any time.
03:29 Let me add day 6 in the array.
03:33 Place the cursor at the end of the last line and press Enter.

Then type the following.

03:42 Save the file.
03:44 We have declared the array.

But how should we refer to the array element?

03:49 Write the arrayname and the index within square brackets to refer an element, at a particular index.

Let us try this.

03:58 Switch to the code once again.
04:01 Place the cursor in front of the closing curly brace.
04:05 Press Enter and type: print space day within square brackets 6.
04:13 Save the code.
04:15 Open the terminal by pressing Ctrl, Alt and T keys.
04:20 Go to the folder in which you downloaded and extracted the Code Files using 'cd' command.
04:27 Now, type: awk space hyphen small f space array_intro.awk .

Press Enter.

04:38 See, we get Friday as the output.
04:42 Next we will check whether any element exists in an array at a certain index.
04:48 For this, we have to use the in operator. Let me explain this with an example.
04:55 Switch to the code in the editor window.
04:59 Place the cursor at the end of the print statement and press Enter.

Then type as shown.

05:09 Save the code.
05:11 Now I have added two 'if' conditions.
05:15 The first if condition checks whether the index two is present in day.
05:21 If yes, then the corresponding print statement will get executed.
05:26 Then the second condition checks whether the index seven is present in day.

It will execute the print statement if it is true.

05:35 As we can see, index two is in the array and seven is not.

Let’s execute this file to verify the output.

05:44 Switch to the terminal. Press the up arrow key to get back the previously executed command.
05:51 Press Enter to execute.
05:54 We get the output as expected.
05:57 We will now make some more changes to the code.

Update the code as shown here.

06:04 Below the 7 in day condition, I have added one more condition.
06:09 This will check whether the value of index seven is null or not.
06:14 If true, it will print Index 7 is not null.
06:18 We already know that we don’t have any index with 7. So, it will not print anything.
06:24 Next, we have changed the print statement of the condition 7 in day.
06:30 Save the code.

Let’s see what happens when we execute the code.

06:35 Switch to terminal.

Press the up arrow key to get the previously executed command.

06:43 Press Enter to execute.
06:46 We got an unexpected output.
06:49 The statement "Index 7 is present after null comparison." is printed.

How is it possible?

06:57 When we write, day[7] not equal to null, we are trying to access the element at index 7.
07:04 This access itself will first create an element at index 7 and initialize it with the value null.
07:12 Next, we are trying to check if any element is actually present at index 7.
07:18 As null element is already created, the output shows that Index 7 is present after null comparison.
07:26 So, remember this:

day at index 7 not equal to null is a wrong way to check the presence of an element.

07:34 It will create a null element at index 7.
07:38 Instead, we have to use the in operator.
07:41 It will not create any extra element in the array.

This brings us to the end of this tutorial.

07:50 In this tutorial, we learnt about-

Arrays in awk,

07:54 Assigning array elements,
07:56 How it is different from arrays in other programming languages,
08:00 Refer the elements of an array .
08:03 As an assignment-

Define an array flowerColor.

08:07 Index will be the names of the flowers.
08:10 Value will be the corresponding color of the flowers.
08:14 Insert entries for any five flowers of your choice.
08:18 Print the color of the fourth flower.

Check if the flower “Lotus” is present in the array.

08:25 The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

08:33 The Spoken Tutorial Project team conducts workshops using spoken tutorials.

And, gives certificates on passing online tests.

08:42 For more details, please write to us.
08:46 Please post your timed queries in this forum.
08:50 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

09:01 The script has been contributed by Antara. And, this is Praveen from IIT Bombay, signing off.

Thanks for joining

Contributors and Content Editors

PoojaMoolya, Sandhya.np14