Thursday, June 19, 2014

awk: Program Elements(4)

1. Array Variables
Single Dimension Array:
 #! /bin/bash  
   
 #awk array could use either number or string expressions  
 #as indexes, and the value can be a mix of number or  
 #expression  
 #Efficiency, no matter how many entities the array has,  
 #the find, insert, delete could be finished in constant  
 #time.  
 echo "" | awk '{  
   phonebook["bb"]=718;  
   phonebook["yy"]="213";  
   
   print phonebook["bb"], phonebook["yy"];  
   #output: 718 213  
 }'  
   
 #awk array use sparse storage. For following example, we  
 #don't need to store data for element from 1 to 9.  
 echo "" | awk '{  
   phonebook[0]=213;  
   phonebook[10]=718;  
    
   print phonebook[0], phonebook[10];  
   #output: 213 718  
 }'  
   
 #delete array and array elements  
 echo "" | awk '{  
   a[0] = 0;  
   a[1] = 1;  
   print a[0], a[1];  
   #output: 0 1  
   
   delete a[0];  
   delete a[1];  
   print a[0], a[1];  
   #output:  (empty)  
   #From technical perspective, element does not exist any   
   #more after deletion. The memory is reclaimed for these  
   #two elements.  
   
   a[0] = 0;  
   a[1] = 1;  
   delete a; #delete the entire array, all memory is reclaimed  
   print a[0], a[1];  
   #output:  (empty)  
 }'  
   
 #conversion between array type and "string/number" type  
 echo "" | awk '{  
   a[0] = 0;  
   a[1] = 1;  
   delete a;  
   a="Hello world"; #error  
   #deleting array does not mean deleting the variable name  
   #At underlying strucuture, variable name a is created with  
   #type "array", we could only assign array to "a" after a is  
   #created.  
   
   s="Hello world";  
   print s;  
   delete s; #error, delete could only apply on array  
   s[0] = 0; #error, s is created with type string/number, we could   
        #only assign string to s  
   s=100; #this is ok  
   print s;  
 }'  
   

Multiple Dimension Array:
 #! /bin/bash  
   
 echo "" | awk '{  
   arr[0,0]="Hello";  
   arr[0,1]="world!";  
   arr[1,0]="Hello";  
   arr[1,1]="Chicago";  
   
   print arr[0,0],arr[0,1],arr[1,0], arr[1,1];  
   #output: Hello world! Hello Chicago  
   
   print arr[0 SUBSEP 0], arr["0" SUBSEP 1], \  
      arr[1 "\034" 0], arr["1\0341"]  
   #output: Hello world! Hello Chicago  
   
   #procedure of converting multiple dimension array  
   #to single dimension array:  
   #awk is using the single dimension array at the underlying  
   #data structure. For arr[0,0], it will firstly convert all  
   #number to string, arr["0","0"]. Then convert "," to   
   #built-in variable SUBSEP, which is unprintable variable  
   #whose ascii code is "\034". So finally, the array reference  
   #becomes: arr["0\0340"], which is a single dimension array  
   #So the 2nd print statement has the same output of 1st print  
   #statement.  
 }'  
   

No comments:

Post a Comment