Introduction to awk Arrays

Source: Internet
Author: User

I. Definition

In awk, an array is an associated array, which features:

1. The subscript of an array can be an integer, a negative number, or a string.

2. The subscript of the array can be discontinuous.

The value of the ignorecase variable of awk does not affect the array subscript.

When an array is created by awk, if no subscript is specified, a continuous integer is used as the subscript by default. The starting value is 1.

What is the efficiency of the awk array? The time for accessing an element is irrelevant to the number of elements.

Note:The subscript of awk is actually a string.For example, if we enter the number 1, awk will automatically convert it to the string "1 ".

II,Reference Element

The basic method for referencing an awk element is:

Array [Index]

Note: In this reference method, unless the variable is written in [], double quotation marks must be added. That is, rules for adding double quotation marks to strings must also be followed in the subscript of the array, however, if the subscript is a full number, no quotation marks can be added, for example:

BB = "123"

Arr [BB] awk recognizes BB as a variable and obtains the value of ARR ["123 "].

Arr [234] awk recognizes 234 as a string, although 234 is not enclosed in quotation marks. Because the variable definition cannot use full numbers, 234 must be a string instead of a variable.

Arr ["BB"] awk recognizes BB as a string and obtains the value of ARR ["BB "].

Sometimes, the value of the array is null. The element with null value contains two conditions::1. The value itself is null.;2. Delete the deleted element. Both cases can be referenced normally. Unfortunately, if an element that does not exist is referenced, The awk creates this element and the value is null. This will cause a waste of awk memory.

To query whether an element with an index value exists, use the following expression:

Index in array

This expression only tests whether the element with index exists in the array. If it does not exist, this expression will not generate an element with the index as the underlying value as null.

If an element exists, this expression returns 1 (true)

Otherwise, 0 (false) is returned)

For example, the test in the array frequencies isNo element with subscript 2 exists

If (2 in frequencies)

Print "Sub 2 is present ."

Note: Do not test whether there are elements with value 2 in frequencies, unless all elements in the scan array.

III,Assign values to elements

Assign a value to the awk array in the following format:

Array [sub] = Value

Array: array name

Sub: subscript

Value: Value

Note: awk can generate null subscript elements, but the reference method must be arr [""] instead of ARR []..

Length () Returns the number of elements in the array.

4. Traverse Arrays

When using the ArrayProgramIn, a loop is often used to make each element in the array perform an operation at a time. In other programs, the subscript of the array is a continuous positive integer, so all the subscript can be easily traversed from low to high. However, this method cannot be used in awk, because the subscript of awk can be a number or a string. Therefore, awk uses a special statement to traverse elements in the array:

For (VAR in array)

Body

The above loop enables each element in the array to execute the body once.

In the first part of the following program, every word in the input text is saved as a subscript into an array. If a word is repeated, only one word is retained, because the subscript of the array cannot be repeated.

5. Delete Elements

Delete a single element

Delete array [Index]

Method 1 of deleting the entire array

For (VAR in array)

Delete array [Var]

Method 2 for deleting the entire array (this method is dedicated to gawk and has poor portability, but the efficiency is about three times that of method 1)

Delete Array

The awk array and variable use the same address space. The array name and variable cannot be the same. The name cannot be used for variable naming even if the array is deleted. The following command reports an error:

A [1] = 3; Delete A; A = 3

Vi. array assignment

Single Value assignment: tarray [1] ="CHENG Mo"Tarray [2] ="800927"

multiple values: awk 'in in {info = "it is a test"; lens = Split (Info, Ta ,""); Print length (TA), lens, Ta [1];} '

Original article:

Http://blog.csdn.net/xrzs1986/article/details/6261926

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.