Delete operation in arrays

The delete command is used to remove the individual element from an array. Once an element from an AWK array is deleted, we cannot obtain its value any longer. The syntax of the delete statement is as follows:

delete arr[index];

In the following example, we delete the array element with the car index and print all the remaining elements using the for loop, as follows:

$ vi arr_delete.awk

BEGIN {
arr[10] = "maruti"
arr[20] = "audi"
arr["car"] = "ford"
arr[30] = "ferrari"
arr[40] = "porsche"

delete arr["car"]
for ( v in arr )
print v,arr[v]
}

$ awk -f arr_delete.awk

The output of the execution of the preceding code is as follows:

10 maruti
20 audi
30 ferrari
40 porsche

The following for loop command removes all elements from an arr array:

for (v in arr)
 delete arr[v]

Now, we delete all the elements of the array using the loop and print the array elements again. It will not display anything as all elements in the array are deleted, as follows:

$ vi arr_del_forloop.awk

BEGIN {
arr[10] = "maruti"
arr[20] = "audi"
arr["car"] = "ford"
arr[30] = "ferrari"
arr[40] = "porsche"

for ( v in arr )
delete arr[v]
for ( v in arr )
        print arr[v]
}

$ awk -f arr_del_forloop.awk

It will not give any output of the execution of the preceding code, since all elements of the arrays are deleted and the arr array is now empty.

Once an element is deleted, a subsequent loop iteration used to scan the array will not report that element, and using the in operator to check for the existence of that element will return zero—that is, false. For example, in the following code, we delete an array element and then use the if conditional statement to check for its existence:

delete arr[20]
if ( 20 in arr )
print "index 20 found"
else
print "20 not found"

 Deleting an element is not the same as assigning it a null value string (the empty string " "). For example:

arr[20] = " "
if ( 20 in arr )
print "20 is array, although arr[20] is empty"

Also, if we try to delete an element that does not exist, it is not treated as an error. Furthermore, if you want to delete all the elements of the array in a single command, we can use the delete command without any subscripts, as follows:

$ vi arr_delall.awk

BEGIN {
arr[10] = "maruti"
arr[20] = "audi"
arr["car"] = "ford"
arr[30] = "ferrari"
arr[40] = "porsche"

delete arr
print "List of elements in array is : "
for ( v in arr )
print arr[v]
}

$ awk -f arr_delall.awk

The output of the execution of the preceding code is as follows:

List of elements in array is : 

Hence, if you want to delete all the elements of an array, using the delete statement alone is much more efficient than using an equivalent loop to delete each element one at a time. This form of delete statement is also supported by most types of implementation of AWK, such as MAWK, NAWK, and so on.

Deleting all elements from an array does not change its type and make it available for use as a regular variable. For example, the following code will throw the error and not work:

$ vi arr_delete_error.awk

BEGIN {
arr[10] = "audi";
delete arr;
arr = "bmw";
print arr;
}

$ awk -f arr_delete_error.awk

The output of the execution of the preceding code is as follows:

awk: arr_delete_err.awk:4: fatal: attempt to use array `arr' in a scalar context
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.5.12