Difference between revisions of "Linux-AWK/C2/MultiDimensional-Array-in-awk/English"
Nancyvarkey (Talk | contribs) m (Nancyvarkey moved page Linux/C3/MultiDimensional-Array-in-awk/English to Linux-AWK/C2/MultiDimensional-Array-in-awk/English without leaving a redirect: New series) |
|||
(One intermediate revision by the same user not shown) | |||
Line 62: | Line 62: | ||
− | For example, '''array week '''is identified by a single '''index | + | For example, '''array week '''is identified by a single '''index, day'''. |
|- | |- | ||
Line 73: | Line 73: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 6a | | style="background-color:#ffffff;border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 6a | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here, '''multiple indices '''are concatenated into a single '''string''', | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here, '''multiple indices '''are concatenated into a single '''string''', with a '''separator '''between them. |
− | + | ||
− | + | ||
− | with a '''separator '''between them. | + | |
Line 83: | Line 80: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 6b | | style="background-color:#ffffff;border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide 6b | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The combined '''string''' is used as a single '''index''' for a simple '''one dimensional array'''. |
|- | |- | ||
Line 90: | Line 87: | ||
− | + | Suppose we write '''multi within square brackets 4 comma 6 equal to value in double quotes''' | |
Here '''multi '''is the name of '''multi-dimensional array. ''' | Here '''multi '''is the name of '''multi-dimensional array. ''' | ||
Line 97: | Line 94: | ||
Then, the numbers '''4 '''and''' 6 '''are converted to a '''string'''. | Then, the numbers '''4 '''and''' 6 '''are converted to a '''string'''. | ||
− | Suppose, | + | Suppose, the value of '''SUBSEP''' is '''hash symbol (#).''' |
− | + | ||
− | + | ||
− | the value of '''SUBSEP''' is '''hash symbol (#).''' | + | |
Line 178: | Line 172: | ||
− | And then type '''print space a | + | And then type '''print space a within square brackets 2 comma 2''' |
Line 194: | Line 188: | ||
We can use '''in operator.''' | We can use '''in operator.''' | ||
− | We have already seen it in '''single-dimensional array '''earlier | + | We have already seen it in '''single-dimensional array '''earlier in this series. |
− | We have to write the entire '''sequence '''of '''indices '''within '''parentheses ''' | + | We have to write the entire '''sequence '''of '''indices '''within '''parentheses ''' and separated by '''commas'''. |
− | + | Let us see this in an example. | |
− | + | ||
− | + | ||
− | Let us see this in an example | + | |
|- | |- | ||
Line 252: | Line 243: | ||
awk -f test_multi.awk | awk -f test_multi.awk | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to the '''terminal '''and type | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to the '''terminal '''and type- |
− | '''awk space hyphen small f space | + | '''awk space hyphen small f space test underscore multi dot awk '''and press '''Enter.''' |
|- | |- | ||
Line 370: | Line 361: | ||
} | } | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| So, after '''awk '''has processed the entire '''input file '''with this code, | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| So, after '''awk '''has processed the entire '''input file '''with this code, '''matrix array '''will be completely formed. |
− | |||
− | + | It will store entire data of '''input file '''into a '''two dimensional array''' format. | |
− | It will store entire data of '''input file ''' | + | |
|- | |- | ||
Line 401: | Line 390: | ||
− | I assume your familiarity with basic C programming. | + | I assume your familiarity with basic '''C programming'''. |
Line 417: | Line 406: | ||
− | '''Awk''' does not have '''multi-dimensional array''' in the truest sense. | + | '''Awk''' does not have a '''multi-dimensional array''' in the truest sense. |
Line 454: | Line 443: | ||
− | The first piece is stored in ''' | + | The first piece is stored in ''' arr 1'''. |
− | The second piece in ''' | + | The second piece in '''arr 2''' and so forth. |
|- | |- | ||
Line 477: | Line 466: | ||
| style="background-color:#ffffff;border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <nowiki>Highlight a[1,1]="A";a[1,2]="B";a[2,1]="C"; a[2,2]="D"</nowiki> | | style="background-color:#ffffff;border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <nowiki>Highlight a[1,1]="A";a[1,2]="B";a[2,1]="C"; a[2,2]="D"</nowiki> | ||
| style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| First we have created an '''array '''named '''a '''and assigned these values to it. | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| First we have created an '''array '''named '''a '''and assigned these values to it. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 492: | Line 478: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight split(iterator,arr,SUBSEP) | | style="background-color:#ffffff;border-top:0.035cm solid #000001;border-bottom:0.035cm solid #000001;border-left:0.035cm solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight split(iterator,arr,SUBSEP) | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Then the '''split function '''breaks the '''iterator '''into pieces separated by | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Then the '''split function '''breaks the '''iterator '''into pieces separated by '''SUBSEP.''' |
|- | |- | ||
Line 509: | Line 495: | ||
<nowiki>[Enter]</nowiki> | <nowiki>[Enter]</nowiki> | ||
− | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to the '''terminal '''and type | + | | style="background-color:#ffffff;border:0.035cm solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to the '''terminal '''and type- |
− | '''awk space hyphen small f space | + | '''awk space hyphen small f space multi underscore scan dot awk ''' |
and press '''Enter.''' | and press '''Enter.''' | ||
Line 530: | Line 516: | ||
* Create a '''multidimensional array '''in '''awk''' | * Create a '''multidimensional array '''in '''awk''' | ||
* and scan a '''multidimensional array''' | * and scan a '''multidimensional array''' | ||
− | |||
− | |||
|- | |- | ||
Line 544: | Line 528: | ||
* rotate a '''two dimensional array '''by 90 degree | * rotate a '''two dimensional array '''by 90 degree | ||
* and print the rotated '''matrix'''. | * and print the rotated '''matrix'''. | ||
− | |||
− | |||
|- | |- |
Latest revision as of 16:38, 22 March 2018
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 |