DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

(gawk.info.gz) Concatenation

Info Catalog (gawk.info.gz) Arithmetic Ops (gawk.info.gz) Expressions (gawk.info.gz) Assignment Ops
 
 String Concatenation
 ====================
 
      It seemed like a good idea at the time.
      Brian Kernighan
 
    There is only one string operation: concatenation.  It does not have
 a specific operator to represent it.  Instead, concatenation is
 performed by writing expressions next to one another, with no operator.
 For example:
 
      $ awk '{ print "Field number one: " $1 }' BBS-list
      -| Field number one: aardvark
      -| Field number one: alpo-net
      ...
 
    Without the space in the string constant after the `:', the line
 runs together.  For example:
 
      $ awk '{ print "Field number one:" $1 }' BBS-list
      -| Field number one:aardvark
      -| Field number one:alpo-net
      ...
 
    Because string concatenation does not have an explicit operator, it
 is often necessary to insure that it happens at the right time by using
 parentheses to enclose the items to concatenate.  For example, the
 following code fragment does not concatenate `file' and `name' as you
 might expect:
 
      file = "file"
      name = "name"
      print "something meaningful" > file name
 
 It is necessary to use the following:
 
      print "something meaningful" > (file name)
 
    Parentheses should be used around concatenation in all but the most
 common contexts, such as on the righthand side of `='.  Be careful
 about the kinds of expressions used in string concatenation.  In
 particular, the order of evaluation of expressions used for
 concatenation is undefined in the `awk' language.  Consider this
 example:
 
      BEGIN {
          a = "don't"
          print (a " " (a = "panic"))
      }
 
 It is not defined whether the assignment to `a' happens before or after
 the value of `a' is retrieved for producing the concatenated value.
 The result could be either `don't panic', or `panic panic'.  The
 precedence of concatenation, when mixed with other operators, is often
 counter-intuitive.  Consider this example:
 
      $ awk 'BEGIN { print -12 " " -24 }'
      -| -12-24
 
    This "obviously" is concatenating -12, a space, and -24.  But where
 did the space disappear to?  The answer lies in the combination of
 operator precedences and `awk''s automatic conversion rules.  To get
 the desired result, write the program in the following manner:
 
      $ awk 'BEGIN { print -12 " " (-24) }'
      -| -12 -24
 
    This forces `awk' to treat the `-' on the `-24' as unary.
 Otherwise, it's parsed as follows:
 
          -12 (`" "' - 24)
      => -12 (0 - 24)
      => -12 (-24)
      => -12-24
 
    As mentioned earlier, when doing concatenation, _parenthesize_.
 Otherwise, you're never quite sure what you'll get.
 
Info Catalog (gawk.info.gz) Arithmetic Ops (gawk.info.gz) Expressions (gawk.info.gz) Assignment Ops
automatically generated byinfo2html