title: Iterate over list of known characters
body: <p>To iterate over a list of characters that are known at the time of writing the program (in this example, the characters are "X", "Y", "Z"):</p>
  
  <pre><code>for (i = 1; i &lt;= 3; ++i) {
      c = substr("XYZ", i, 1)
      # do something with the character
  }
  </code></pre>
  
  <p><strong>Question:</strong> <em>Is there a more awk-y way of doing this?</em> Note that this is not the same as <a href="https://stackoverflow.com/questions/8563745/loop-over-characters-in-input-string-using-awk">this question</a>, as the characters I want to iterate over are not a part of the input.</p>
  
  <p>To put it in context, I need to count the occurences of X, Y and Z on a particular position in a line over all lines. The input should consist only of X, Y and Zs on lines of the same length:</p>
  
  <pre><code>$ cat input.txt
  XYXXXYZZYXY
  XXXYYYZYYZY
  YZZZZYZZXZZ
  XXZXXYYZXZY
  
  $ foo.awk &lt; input.txt
  X 3 2 2 2 2 0 0 0 2 1 0
  Y 1 1 0 1 1 4 1 1 2 0 3
  Z 0 1 2 1 1 0 3 3 0 3 1
  </code></pre>
  
  <p>This is <code>foo.awk</code> at the moment:</p>
  
  <pre><code>#!/bin/awk -f
  BEGIN {
      FS = ""
  }
  NR == 1 {
      len = NF
  }
  {
      for (i = 1; i &lt;= NF; ++i)
          ++profile[$i][i]
  }
  END {
      for (c = 1; c &lt;= 3; ++c) {
          char = substr("XYZ", c, 1)
          printf "%s", char
          for (i = 1; i &lt;= len; ++i)
              printf " %d", profile[char][i]
          printf "\n"
      }
  }
  </code></pre>
  
  <p>I have not used awk before so probably my whole approach is totally wrong.</p>