title: why isn't this awk script behaving as expected?
body: <p>I have the following test script</p>
  
  <pre><code> /^[^a-zA-Z0-9]/  {
      DATEd[$3] = $1
      } 
     END { 
          print "        \"data\": ["
          for (i = 0 ; i &lt;= 5; i ++ ) {
              { print "            [" i ", \"" DATEd[i] "\"],"}
          }
          print "        ]"
  }
  </code></pre>
  
  <p>And are reading from this text file</p>
  
  <pre><code>2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
  2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399 
  2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399 
  2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
  2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399 
  2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
  </code></pre>
  
  <p>But it doesn't print out what I want it to, I want it to print out</p>
  
  <pre><code>    "data": [
          [0, "2011-01-22"],
          [1, "2011-01-22"],
          [2, "2011-01-22"],
          [3, "2011-01-22"],
          [4, "2011-01-22"],
          [5, "2011-01-22"],
      ]
  </code></pre>
  
  <p>When it in fact are only printing out</p>
  
  <pre><code>"data": [
      [0, ""],
      [1, ""],
      [2, ""],
      [3, ""],
      [4, ""],
      [5, ""],
  ]
  </code></pre>
  
  <p>So why is "DATEd[$3] = $1" empty?</p>
  
  <p>Also how do I check the length of an array?
  DATEd.length doesn't work in this case.</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT_<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>___</strong></p>
  
  <p>So from the help of @Fredrik and @geekosaur I have come somewhere with this, now to some last questions</p>
  
  <p>1) The script now looks like this</p>
  
  <pre><code> /[a-zA-Z0-9]/  {
      DATEd[NR-1] = $1
      } 
     END { 
          print "        \"data\": ["
  
          for (i in DATEd) {
              { print "            [" i ", \"" DATEd[i] "\"],"}
          }
          print "        ]"
  }
  </code></pre>
  
  <p>And gives the following output</p>
  
  <pre><code>"data": [
      [4, "2011-01-26"],
      [5, "2011-01-27"],
      [6, "2011-01-28"],
      [0, "2011-01-22"],
      [1, "2011-01-23"],
      [2, "2011-01-24"],
      [3, "2011-01-25"],
  ]
  </code></pre>
  
  <p>But I want it to look like this</p>
  
  <pre><code>"data": [
  [0, "2011-01-22"],
  [1, "2011-01-23"],
  [2, "2011-01-24"],
  [3, "2011-01-25"],
  [4, "2011-01-26"],
  [5, "2011-01-27"],
  [6, "2011-01-28"]
  ]
  </code></pre>
  
  <p>I.E be sorted and removing the last ',' character before the final closing ']' character. Is this possible to accieve in a easy way? =)</p>
  
  <p>Thanks =)</p>
  
  <p><strong>EDIT 3 Final Outcome_<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>____</strong></p>
  
  <p>Used a combination of @geekosaur and @Fredrik contribution's =)</p>
  
  <pre><code>{
      DATEd[NR-1] = $1; len++
  }
     END { 
          print "        \"data\": ["
  
          #for (i in DATEd) {
          for (i = 0 ; i &lt;= len-1; i ++ ) {
              { print "            [" i ", \"" DATEd[i] "\"],"}
          }
          print "        ]"
  }
  </code></pre>