Linux-AWK/C2/MultiDimensional-Array-in-awk/English
Title of script: Multi-Dimensional array in awk
Author: Antara Roy Choudhury
Keywords: Multi-dimensional array, SUBSEP, Multi-dimensional scanning, Creating transpose of a matrix
|
|
Display Slide 1 | Welcome to the spoken tutorial on creating multidimensional arrays in awk. |
Display Slide 2 | In this tutorial we will learn to-
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 array on this website.
|
Slide 5: Code Files | The files used in this tutorial are available in the Code Files link on this tutorial page.
|
Display Slide 5a | What is a multidimensional array in awk?
|
Display Slide 5b | However, in multidimensional array, an element is identified by a sequence of multiple indices.
|
Display Slide 6a | Here, multiple indices are concatenated into a single string, with a separator between them.
|
Display Slide 6b | The combined string is used as a single index for a simple one dimensional array. |
Display slide 7 | For example,
Here multi is the name of multi-dimensional array.
Suppose, the value of SUBSEP is hash symbol (#).
|
Display slide 8 | So, the array element multi within square brackets within double quotes 4 hash 6 is set to value within double quotes.
|
Display slide 9 | Let us try to declare a two dimensional array as shown in the slide.
|
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 at the terminal:
awk 'BEGIN{a[1,1]="A";a[1,2]="B";a[2,1]="C"; a[2,2]="D"}'
|
Now define the array as follows.
|
Show terminal | We get a command prompt back without any error.
|
Press Up key | Press the Up arrow key to get the previously executed command in the terminal. |
Modify previous code to add the highlighted portion
[Enter] |
Just before the closing curly bracket, type a semicolon.
|
Show the output | Notice, we get the output as capital D. |
Display slide 10 | How to test if a particular index sequence exists in a given multidimensional array?
We have already seen it in single-dimensional array earlier in this series.
|
show test_multi.awk in Gedit | I have already written a script named test_multi.awk
|
Highlight
a[1,1]="A";a[1,2]="B";a[2,1]="C"; a[2,2]="D" |
I have defined a 2 by 2 array as seen in our previous discussion. |
Highlight both the if statement | Then I have written two if conditions. |
Highlight 1st if statement | The first if condition checks whether the element at the index one comma one, is present or not. |
Highlight (1,1) | We have to write the index for multidimensional array within parentheses. |
Highlight print "(1,1) is present" | If the condition is true, it will print one comma one is present. |
Highlight print "(1,1) is absent" | Else it will print one comma one is absent. |
Highlight 2nd if statement
if((3,1) in a) |
Similarly, we will check for the presence of the element at index three comma one.
|
Type:
|
Switch to the terminal and type-
|
Show output | The output says one comma one is present and three comma one is absent. |
Display slide 11 | Let us take one more example.
|
Show 2D-array.txt in gedit | I have created a two-dimensional matrix in the file 2D-array.txt. |
Show transpose.awk in gedit | I have written a code named transpose.awk |
Highlight the 1st block
if (max_nf < NF) max_nf = NF max_nr = NR for (x = 1; x <= NF; x++) matrix[NR, x] = $x } |
First look at the action section of this awk script. |
Highlight if (max_nf < NF)
|
Here we are calculating the maximum number of fields in a row.
|
Highlight max_nr = NR | As we know, NR is the number of current records processed by awk.
|
Display slide 12 | Awk will process the input file from the first record to the last record.
|
Highlight appropriately
for (x = 1; x <= NF; x++) matrix[NR, x] = $x |
Now we should read the data from input file and store the data in a two dimensional array.
|
Highlight
{ if (max_nf < NF) max_nf = NF max_nr = NR for (x = 1; x <= NF; x++) matrix[NR, x] = $x } |
So, after awk has processed the entire input file with this code, matrix array will be completely formed.
|
Highlight
for (col = 1; col <= max_nf; col++) { for (row = 1; row <= max_nr; row++) printf("%s ", matrix[row, col]) printf("\n") }
|
Now, let’s us look inside the END section.
|
Display slide 13a
|
Now, we will learn how to scan a multidimensional array.
|
Display slide 13b | You can have multidimensional way to scan an array.
|
Display slide 14a | Let us see what the split function is.
|
Display slide 14b | The syntax is as follows.
|
Display slide 14c | The third argument mentions the separator that will be used to chop the string up.
|
Retain same screen | Suppose, we want to recover the original sequence of indices from an already created array.
|
Show multi_scan.awk in gedit | I have written a code named multi_scan.awk |
Highlight entire code | Entire code is written inside the BEGIN section. |
Highlight a[1,1]="A";a[1,2]="B";a[2,1]="C"; a[2,2]="D" | First we have created an array named a and assigned these values to it. |
Highlight for loop | Then we have the for loop with an iterator.
Say 1,1 then 1,2 and so on. |
Highlight split(iterator,arr,SUBSEP) | Then the split function breaks the iterator into pieces separated by SUBSEP. |
Highlight arr inside the split function | The pieces will be stored in the array arr. |
Highlight arr[1] "," arr[2] | So, arr[1] and arr[2] will contain the first index and second index respectively.
|
Type: awk -f multi_scan.awk
[Enter] |
Switch to the terminal and type-
awk space hyphen small f space multi underscore scan dot awk and press Enter. |
Show output | See the output; the original sequence of indices are recovered. |
Display Slide 15
Summary |
Let us summarize.
|
Display Slide 16
Assignment
|
As an assignment, write an awk script to
|
Display Slide 17
About Spoken Tutorial project |
The video at the following link summarises the Spoken Tutorial project.
|
Display Slide 18
Spoken Tutorial workshops |
The Spoken Tutorial Project team conducts workshops using spoken tutorials.
|
Display Slide 19
Forum for specific questions: |
Please post your timed queries in this forum. |
Display Slide 20
Acknowledgement |
Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
this link. |
The script has been contributed by Antara.
Thank you for joining |