21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12: A Library of awk Functions 195There is a “test program” in a BEGIN rule, to test the function. It is commented out forproduction use.12.2.7 Merging an Array into a StringWhen doing string processing, it is often useful to be able to join all the strings in an arrayinto one long string. The following function, join, accomplishes this task. It is used laterin several of the application programs (see Chapter 13 [Practical awk Programs], page 215).Good function design is important; this function needs to be general but it should alsohave a reasonable default behavior. It is called with an array as well as the beginning andending indices of the elements in the array to be merged. This assumes that the arrayindices are numeric—a reasonable assumption since the array was likely created with split(see Section 8.1.3 [String-Manipulation Functions], page 132):# join.awk --- join an array into a stringfunction join(array, start, end, sep, result, i){if (sep == "")sep = " "else if (sep == SUBSEP) # magic valuesep = ""result = array[start]for (i = start + 1; i

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

Saved successfully!

Ooh no, something went wrong!