21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8: Functions 133a[2] = "de"a[3] = "sac"The asort function is described in more detail in Section 7.11 [Sorting ArrayValues and Indices with gawk], page 127. asort is a gawk extension; it is notavailable in compatibility mode (see Section 11.2 [Command-Line Options],page 177).asorti(source [, dest]) #asorti is a gawk-specific extension, returning the number of elements in thearray source. It works similarly to asort, however, the indices are sorted,instead of the values. As array indices are always strings, the comparisonperformed is always a string comparison. (Here too, IGNORECASE affects thesorting.)The asorti function is described in more detail in Section 7.11 [Sorting ArrayValues and Indices with gawk], page 127. It was added in gawk 3.1.2. asortiis a gawk extension; it is not available in compatibility mode (see Section 11.2[Command-Line Options], page 177).index(in, find)This searches the string in for the first occurrence of the string find, and returnsthe position in characters where that occurrence begins in the string in.Consider the following example:$ awk ’BEGIN { print index("peanut", "an") }’⊣ 3If find is not found, index returns zero. (Remember that string indices in awkstart at one.)length([string])This returns the number of characters in string. If string is a number, thelength of the digit string representing that number is returned. For example,length("abcde") is 5. By contrast, length(15 * 35) works out to 3. In thisexample, 15 * 35 = 525, and 525 is then converted to the string "525", whichhas three characters.If no argument is supplied, length returns the length of $0.NOTE: In older versions of awk, the length function could be calledwithout any parentheses. Doing so is marked as “deprecated” inthe POSIX standard. This means that while a program can dothis, it is a feature that can eventually be removed from a futureversion of the standard. Therefore, for programs to be maximallyportable, always supply the parentheses.If length is called with a variable that has not been used, gawk forces thevariable to be a scalar. Other implementations of awk leave the variable withouta type. Consider:$ gawk ’BEGIN { print length(x) ; x[1] = 1 }’⊣ 0error gawk: fatal: attempt to use scalar ‘x’ as array

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!