title: Avoid empty element in list after reading file using 'split'
body: <p>While reading a file line by line, with the help of <code>split</code>, we used to follow the conventional way as below</p>
  
  <pre><code>set fp [ open "test" r ]
  set data [ read $fp ]
  set output [ split $data \n ]
  close $fp
  </code></pre>
  
  <p>The content of the file <code>test</code> as below.</p>
  
  <pre><code>a
  b
  c
  d
  </code></pre>
  
  <p><strong>Note</strong> : I have not typed 'enter' key at the end of the file which is near the letter 'd'. </p>
  
  <p>Now I am printing the length of the list after reading it with the way shown the beginning.</p>
  
  <pre><code>puts [llength $output ]; # Printing length of the file data's list length - giving 5
  </code></pre>
  
  <p>With respect to <code>split</code>, if there is any delimiter character at the end of the input, then it will obviously create one empty element in that list thereby increasing the size of the list by 1.</p>
  
  <p>Consider the below examples. </p>
  
  <pre><code>puts [llength [ split {a,b,c,d} , ]]; # Will produce list of length 4
  puts [llength [ split {a,b,c,d,} , ]]; # Will produce list of length 5
  </code></pre>
  
  <p>As you can see, the 1st example will produce length as 4, whereas the 2nd example will produce length as 5. This is because, in the 2nd example, I have one comma at the end, thereby making an empty element in the list.</p>
  
  <p>Now, let's come back to the <code>newline</code> case. If I have entered <code>newline</code>, then only it can produce one more empty element. But, even though I am cautious on that, still getting the empty element added up in the list. </p>
  
  <p>Instead of reading from file, if I use split on a string which contains <code>newline</code>, then this issue is not seen. </p>
  
  <pre><code>% set input "a
  b
  c
  d"
  a
  b
  c
  d
  % llength [ split $input \n ]
  4
  % 
  </code></pre>
  
  <p>What is my mistake here ? How to avoid these ? </p>