DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(gawk) Join Function

Info Catalog (gawk) Ordinal Functions (gawk) Library Functions (gawk) Mktime Function
 
 Merging an Array Into a String
 ==============================
 
    When doing string processing, it is often useful to be able to join
 all the strings in an array into one long string.  The following
 function, `join', accomplishes this task.  It is used later in several
 of the application programs ( Practical `awk' Programs Sample
 Programs.).
 
    Good function design is important; this function needs to be
 general, but it should also have a reasonable default behavior.  It is
 called with an array and the beginning and ending indices of the
 elements in the array to be merged.  This assumes that the array
 indices are numeric--a reasonable assumption since the array was likely
 created with `split' ( Built-in Functions for String Manipulation
 String Functions.).
 
      # join.awk --- join an array into a string
      # Arnold Robbins, arnold@gnu.org, Public Domain
      # May 1993
      
      function join(array, start, end, sep,    result, i)
      {
          if (sep == "")
             sep = " "
          else if (sep == SUBSEP) # magic value
             sep = ""
          result = array[start]
          for (i = start + 1; i <= end; i++)
              result = result sep array[i]
          return result
      }
 
    An optional additional argument is the separator to use when joining
 the strings back together.  If the caller supplies a non-empty value,
 `join' uses it.  If it is not supplied, it will have a null value.  In
 this case, `join' uses a single blank as a default separator for the
 strings.  If the value is equal to `SUBSEP', then `join' joins the
 strings with no separator between them.  `SUBSEP' serves as a "magic"
 value to indicate that there should be no separation between the
 component strings.
 
    It would be nice if `awk' had an assignment operator for
 concatenation.  The lack of an explicit operator for concatenation
 makes string operations more difficult than they really need to be.
 
Info Catalog (gawk) Ordinal Functions (gawk) Library Functions (gawk) Mktime Function
automatically generated byinfo2html