title: add columns in dataframes dynamically with column names as elements in List
body: <p>I have List[N] like below</p>
  
  <pre><code>val check = List ("a","b","c","d")
  </code></pre>
  
  <p>where N can be any number of elements.</p>
  
  <p>I have a <code>dataframe</code> with only column called "value". Based on the contents of value i need to create N columns with column names as elements in the list and column contents as <code>substring(x,y)</code></p>
  
  <p>I have tried all possible ways, like <code>withColumn</code>, <code>selectExpr</code>, nothing works.
  Please consider <code>substring(X,Y)</code> where X and Y as some numbers based on some metadata</p>
  
  <p>Below are my different codes which I tried, but none worked,</p>
  
  <hr>
  
  <pre><code>val df = sqlContext.read.text("xxxxx")
  val coder: (String =&gt; String) = (arg: String) =&gt; {
  val param = "NULL"
  if (arg.length() &gt; Y )
  arg.substring(X,Y)
  else
  val sqlfunc = udf(coder)
  val check = List ("a","b","c","d")
  for (name &lt;- check){val testDF2 = df.withColumn(name, sqlfunc(df("value")))}
  </code></pre>
  
  <p>testDF2 has only last column d and other columns such as a,b,c are not added in table</p>
  
  <hr>
  
  <pre><code>var z:Array[String] = new Array[String](check.size)
  var i=0
  for ( x &lt;- check ) {
  if ( (i+1) == check.size) {
  z(i) = s""""substring(a.value,X,Y) as $x""""
  i = i+1}
  else{
  z(i) = s""""substring(a.value,X,Y) as $x","""
  i = i+1}}
  val zz = z.mkString(" ")
  df.alias("a").selectExpr(s"$zz").show()
  </code></pre>
  
  <p>This throws error</p>
  
  <hr>
  
  <p>Please help how to add columns in DF dynamically with column names as elements in List</p>
  
  <p>I am expecting an Df like below</p>
  
  <pre><code>-----------------------------
  Value| a | b | c | d | .... N
  -----------------------------
  |xxx|xxx|xxx|xxx|xxx|xxxxxx-                
  |xxx|xxx|xxx|xxx|xxx|xxxxxx- 
  |xxx|xxx|xxx|xxx|xxx|xxxxxx-
  -----------------------------
  </code></pre>