title: How to use AWK to print associative array in a loop correctly?
body: <pre><code>Beth    45  0
  Danny   33  0
  Thomas  22  40  
  Mark    65  100 
  Mary    29  121 
  Susie   39  76.5
  Joey    51  189.52
  Peter   23  78.26
  Maximus 34  289.71
  Rebecca 21  45.79
  Sophie  26  28.44
  Barbara 24  107.36
  Elizabeth   35  105.69
  Peach   40  102.69
  Lily    41  123 
  </code></pre>
  
  <p>The above is a data file which has three fields: name, age, salary.</p>
  
  <p>I want to print average salary, number, and names for people aged above 30 and under 30.</p>
  
  <p>In this exercise, I want to practise using strings as subscripts.</p>
  
  <p>Here is my AWK code:</p>
  
  <pre><code>BEGIN { OFS = "\t\t" }   
  {
      if ($2 &lt; 30) 
      {   
          a = "age below 30";
          salary[a] += $NF; 
          count[a]++;
          name[a] = name[a] $1 "\t";
      }   
      else
      {   
          a = "age equals or above 30";
          salary[a] += $NF; 
          count[a]++;
          name[a] = name[a] $1 "\t";
      }   
  }
  
  END {
      for (a in salary)
          for (a in count)
              for (a in name)
              {
                  print "The average salary of " a " is " salary[a] / count[a];
                  print "There are " count[a] " people "  a ; 
                  print "Their names are " name[a];
                  print "********************************************************";
              }
  }
  </code></pre>
  
  <p>The following is the output:</p>
  
  <pre><code>The average salary of age equals or above 30 is 109.679
  There are 9 people age equals or above 30
  Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
  ********************************************************
  The average salary of age below 30 is 70.1417
  There are 6 people age below 30
  Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
  ********************************************************
  The average salary of age equals or above 30 is 109.679
  There are 9 people age equals or above 30
  Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
  ********************************************************
  The average salary of age below 30 is 70.1417
  There are 6 people age below 30
  Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
  ********************************************************
  The average salary of age equals or above 30 is 109.679
  There are 9 people age equals or above 30
  Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
  ********************************************************
  The average salary of age below 30 is 70.1417
  There are 6 people age below 30
  Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
  ********************************************************
  The average salary of age equals or above 30 is 109.679
  There are 9 people age equals or above 30
  Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
  ********************************************************
  The average salary of age below 30 is 70.1417
  There are 6 people age below 30
  Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
  ********************************************************
  </code></pre>
  
  <p>The output is very difficult for me to understand.</p>
  
  <p>What I anticipated should look like this: </p>
  
  <pre><code>The average salary of age equals or above 30 is 109.679
  There are 9 people age equals or above 30
  Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
  ********************************************************
  The average salary of age equals or above 30 is 109.679
  There are 9 people age equals or above 30
  Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
  ********************************************************
  The average salary of age equals or above 30 is 109.679
  There are 6 people age below 30
  Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
  ********************************************************
  The average salary of age equals or above 30 is 109.679
  There are 6 people age below 30
  Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
  ********************************************************
  The average salary of age below 30 is 70.1417
  There are 9 people age equals or above 30
  Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
  ********************************************************
  The average salary of age below 30 is 70.1417
  There are 9 people age equals or above 30
  Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
  ********************************************************
  The average salary of age below 30 is 70.1417
  There are 6 people age below 30
  Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
  ********************************************************
  The average salary of age below 30 is 70.1417
  There are 6 people age below 30
  Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
  ********************************************************
  </code></pre>
  
  <p><strong>So my first question is</strong> :  Where did I understand wrong?</p>
  
  <p><strong>And my second question is</strong> : 
  I actually don't need so many loops.  I just need </p>
  
  <pre><code>The average salary of age equals or above 30 is 109.679
  There are 9 people age equals or above 30
  Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
  ********************************************************
  The average salary of age equals or above 30 is 109.679
  There are 9 people age equals or above 30
  Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
  ********************************************************
  </code></pre>
  
  <p><code>for (a in salary, count, names)</code> doesn't work.  Is there a better way ?</p>