Linux-AWK/C2/Basics-of-Single-Dimensional-Array-in-awk/English
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
|
|
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-
We will do this through some examples. |
Display Slide 3
System requirement |
To record this tutorial, I am using
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.
|
Slide 5: Code Files | The files used in this tutorial are available in the Code Files link on this tutorial page.
|
Display Slide 5 | What is an array in awk?
|
Display Slide 6 | It looks very similar to arrays in any other programming language.
|
Slide 7 | * In programming languages, array index is generally a positive integer.
|
Slide 8 | * But in awk, the index can be anything – any number or a string.
|
Slide 9:
|
This is the syntax of assigning an array element in awk.
|
Slide 10 | Strings have to be written inside double quotes, whether it is a index name or a value.
|
Show array_intro.awk in gedit | I have already written the code and saved it as array_intro.awk
Please download and use it. |
Highlight day[1] = “Sunday” | Here I have taken weekdays as an example and written it inside the BEGIN section.
|
Highlight day[“first”] = “Sunday” | In the array element, I have used a string as the index.
|
Highlight the portion
day["fourth"]="wednesday" day[3]="tuesday" day["third"]="tuesday" |
Notice here, the array elements are not in a sequence.
|
Display slide 11 | * In awk arrays, index need not to be in a sequential manner.
|
Switch to gedit and Type:
day["sixth"]="Friday" |
Let me add day 6 in the array.
|
Press Ctrl+S | Save the file. |
Display slide 12 | We have declared the array.
|
Switch to gedit
Type: print day[6] |
Switch to the code once again.
Place the cursor in front of the closing curly brace.
|
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.
|
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.
|
Highlight Both the if statements
if (7 in day) print "Index7 is present."
|
Now I have added two if conditions.
|
Switch to terminal | Switch to the terminal.
Press the Up arrow key to get back the previously executed command.
|
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"
print "Index 7 is present after null comparison." |
We will now make some more changes to the code.
|
Highlight if (day[7] != "") | Below the 7 in day condition, I have added one more condition.
|
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.
|
Switch to terminal
|
Switch to terminal.
|
Highlight "Index 7 is present after null comparison." | We got an unexpected output.
is printed.
|
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.
|
Highlight
if (7 in day) |
Next, we are trying to check if any element is actually present at index 7.
|
Display slide 13 | So, remember this.
|
Display slide 14 | Instead, we have to use the in operator.
This brings us to the end of this tutorial. |
Display Slide 17
Summary |
In this tutorial we learnt about-
|
Display Slide 18
Assignment
|
As an assignment-
|
Display Slide 19
Assignment(Cont.)
|
* Insert entries for any five flowers of your choice
|
Display Slide 21
About Spoken Tutorial project |
The video at the following link summarises the Spoken Tutorial project.
|
Display Slide 22
Spoken Tutorial workshops |
The Spoken Tutorial Project team conducts workshops using spoken tutorials.
|
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.
this link. |
The script has been contributed by Antara.
Thanks for joining |