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

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Basics of single Dimensional Array in Awk

Author: Antara Roy Choudhury

Keywords: Array, assigning array element, referring array element, checking for presence of an element


Visual Cue
Narration
Display Slide 1 Welcome to this spoken tutorial on Basics of single dimensional array in awk.
Display Slide 2 In this tutorial we will learn about-
  • Arrays in awk
  • Assigning array elements
  • How it is different from arrays in other programming languages and
  • Refer the elements of an array

We will do this through some examples.

Display Slide 3

System requirement

To record this tutorial, I am using
  • Ubuntu Linux 16.04 Operating System and
  • gedit text editor 3.20.1

You can use any text editor of your choice.

Display Slide 4

pre-requisite

To practice this tutorial, you should have gone through previous awk tutorials on our website.


You should have some basic knowledge of any programming language like C or C++.


If not, then please go through the corresponding tutorials on our website.

Slide 5: Code Files The files used in this tutorial are available in the Code Files link on this tutorial page.


Please download and extract them.

Display Slide 5 What is an array in awk?
  • awk supports arrays for storing related elements.
  • Elements can be a number or a string.
  • Arrays in awk are associative.
  • This means that each array element is an index-value pair.
Display Slide 6 It looks very similar to arrays in any other programming language.


But there are some important differences.


  • First, we do not need to declare an array before using it.
  • Also there is no need to specify how many elements the array will contain.
Slide 7
  • In programming languages, array index is generally a positive integer.
  • Usually the index starts from 0, then 1, then 2 and so on.
Slide 8 But in awk, the index can be anything – any number or a string.
Slide 9:


array-name[index] = value

This is the syntax of assigning an array element in awk.


Array name can be any valid variable name.


Here the index can be an integer or a string.

Slide 10 Strings have to be written inside double quotes, whether it is index name or a value.


Let’s understand this with an example.

Show array_intro.awk in gedit I have already written the code and saved it as array_intro.awk


This file is available in the Code Files link below the player.

Please download and use it.

Highlight day[1] = “Sunday” Here I have taken weekdays as an example and written it inside the BEGIN section.


Here, the name of the array is day.


I have set the index as 1 and the value as Sunday.

Highlight day[“first”] = “Sunday” In this array element, I have used a string as the index.


So for index first, the value is Sunday.


The entire array is constructed likewise.

Highlight the portion


day[4]="wednesday"

day["fourth"]="wednesday"

day[3]="tuesday"

day["third"]="tuesday"

Notice here, the array elements are not in a sequence.


I have declared day four before day three.

Display slide 11
  • In awk arrays, index need not to be in a sequential manner.
  • Advantage of associative array is that new pairs can be added at any time.
Switch to gedit and Type:


day[6]="Friday"

day["sixth"]="Friday"

Let me add day 6 in the array.


Place the cursor at the end of the last line and press Enter.


Then type the following

Press Ctrl+S Save the file.
Display slide 12 We have declared the array.


But how should we refer to the array element?


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


Let us try this.

Switch to gedit

Type:

print day[6]

Switch to the code once again.

Place the cursor in front of the closing curly brace.


Press Enter and type print space day within square brackets 6

Press Ctrl+S Save the code.
Open the terminal by pressing Ctrl, Alt and T keys.
cd /<saved folder> Go to the folder in which you downloaded and extracted the Code Files using cd command
Type:

awk -f array_intro.awk

[Enter]

Now type

awk space hyphen small f space array_intro.awk

Press Enter

Show the output See, we get Friday as the output.
Display slide 12 Next we will check whether any element exists in an array at a certain index.


For this, we have to use the in operator.


Let me explain this with an example.

Switch to gedit

Type:

if (2 in day) [Enter]

print "Index 2 is present."

[Enter]

if (7 in day) [Enter]

print "Index7 is present."

Switch to the code in the editor window.


Place the cursor at the end of the print statement and press Enter.


Then type as shown.


Save the code.

Highlight Both the if statements


Highlight if (2 in day)


Highlight print "Index 2 is present."


Highlight

if (7 in day)

print "Index7 is present."

Now I have added two if conditions.


The first if condition checks whether the index two is present in day.


If yes, then the corresponding print statement will get executed.


Then the second condition checks whether the index seven is present in day.


It will execute the print statement if it is true.


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


Let’s execute this file to verify the output.

Switch to terminal Switch to the terminal.

Press the Up arrow key to get back the previously executed command.


Press Enter to execute.

Highlight Index 2 is present. in the output We get the output as expected.
In gedit, type

if (day[7] != "")

print "Index 7 is not null"


if (7 in day)

print "Index 7 is present after null comparison."

We will now make some more changes to the code.


Update the code as shown here.

Highlight if (day[7] != "") Below the 7 in day condition, I have added one more condition.


This will check whether the value of index seven is null or not.


If true, it will print Index 7 is not null


We already know that we don’t have any index with 7, so it will not print anything.

Highlight "Index 7 is present after null comparison." Next, we’ll change the print statement of the condition 7 in day.
Save the code Save the code.


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

Switch to terminal


Press Up arrow key


Press Enter

Switch to terminal.


Press the up arrow key to get the previous command.

Press Enter to execute.

Highlight "Index 7 is present after null comparison." We got an unexpected output.


The statement "Index 7 is present after null comparison."

is printed.


How is it possible?

Switch to gedit window

Highlight

if (day[7] != "")

When we write, day[7] not equal to null, we are trying to access the element at index 7.


This access itself will first create an element at index 7 and initialize it with the value null.

Highlight

if (7 in day)

Next, we are trying to check if any element is actually present at index 7.


As null element is already created, the output shows that Index 7 is present after null comparison.

Display slide 13 So, remember this.


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


It will create a null element at index 7.

Display slide 14 Instead, we have to use the in operator.


It will not create any extra element in the array.

This brings us to the end of this tutorial.

Display Slide 17

Summary

In this tutorial we learnt about-
  • Arrays in awk
  • Assigning array elements
  • How it is different from arrays in other programming languages
  • Refer the elements of an array
Display Slide 18

Assignment


As an assignment-
  • Define an array flowerColor
  • Index will be the names of the flowers
  • Value will be the corresponding color of the flowers
Display Slide 19

Assignment(Cont.)


  • Insert entries for any five flowers of your choice
  • Print the color of the fourth flower
  • Check if the flower “Lotus” is present in the array
Display Slide 21

About Spoken Tutorial project

The video at the following link summarises the Spoken Tutorial project.


Please download and watch it.

Display Slide 22

Spoken Tutorial workshops

The Spoken Tutorial Project team conducts workshops using spoken tutorials.


And gives certificates on passing online tests.


For more details, please write to us.

Display Slide 23

Forum for specific questions:

Please post your timed queries in this forum.
Display Slide 25

Acknowledgement

Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.


More information on this mission is available at this link.

The script has been contributed by Antara.


And this is Praveen from IIT Bombay signing off.

Thanks for joining

Contributors and Content Editors

Antarade, Nancyvarkey