Linux-AWK/C2/Basics-of-Single-Dimensional-Array-in-awk/English-timed

From Script | Spoken-Tutorial
Revision as of 18:07, 22 July 2019 by Sandhya.np14 (Talk | contribs)

Jump to: navigation, search
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 Element 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