title: How do I select specific columns with the perl -lane command line?
body: <p>The command line below works for me</p>
  
  <pre><code>perl -F'\t' -lane'print join ",", @F[1,2]' inputfile
  </code></pre>
  
  <p>BUT I want to pass a variable list of columns, not necessarily columns 1 and 2 as specified in <code>@F[1,2]</code>.</p>
  
  <p>For example, based on the total number of columns of the inputfile, I would like to select a random subset <code>"$random-columns"</code> and pass it to <code>@F[$random-columns]</code>.</p>
  
  <p>How do I do that?</p>
  
  <p>I tried to first generate a columnList of 5 random column numbers between 1 and 50:</p>
  
  <pre><code>columnList=()
  for (( i = 0; i &lt;= 5-1; ++i ))
  do
      (( randCol = ($RANDOM % 50) + 1 ))
      columnList[i]=$randCol
  done
  </code></pre>
  
  <p>Then I did the following to insert the comma:</p>
  
  <pre><code>cols_new=$(IFS=,; echo "${columnList[*]}")
  </code></pre>
  
  <p>and tried to pass it to the perl command line as below (didn't work):</p>
  
  <pre><code>perl -F'\t' -lane'print join ",", @F[$cols_new]' inputfile
  </code></pre>