title: How to sort an array in tcl?
body: <p>I have an array of this type : </p>
  
  <pre><code>cat1 A B C 12
  cat1 E F G 10
  cat2 C F G 4
  cat2 H F S 12
  cat3 F G M -12
  ...
  </code></pre>
  
  <p>I don't know how many categries I have for input.</p>
  
  <p>I want to have in the output one file by category, sorted with the last column</p>
  
  <pre><code>cat1 E F G 10
  cat1 A B C 12
  </code></pre>
  
  <p>for the first file</p>
  
  <pre><code>cat2 C F G 4
  cat2 H F S 12
  </code></pre>
  
  <p>for the second file, etc ....</p>
  
  <p>I triied this : </p>
  
  <p>i put every line in a $list {{cat1 A B C 12} {cat1 E F G 10} .....}</p>
  
  <p>After, I sort it : </p>
  
  <p>set list [lsort -index 4 -real $list]</p>
  
  <pre><code>foreach element $list {
              regexp {.*} [lindex [split $element " "] 0] start
              set table_cat($start) "$element"
      }
  </code></pre>
  
  <p>After, i write the files : </p>
  
  <pre><code>    foreach element [array names table_cat] {
              set thefile [open "cat_$element.txt" "w"]
              puts $thefile "$table_cat($element)"
      }
  </code></pre>
  
  <p>But I have only 1 line in the result line !</p>
  
  <p>Why ?</p>