text: Q:
  
  Iterating through array with condition to create new array
  
  How can I iterate through an array and assign its val?
  I tried the following two approaches but they did not work:
  var a : Array[String] = Array("foo","bar")
  var b : Array[String] = Array()
  for (i <- 0 to (a.length-1)) {
      println(a(i))
      b :+ a(i)
  }
  
  and
  var a : Array[String] = Array("foo","bar")
  var b : Array[String] = Array()
  for (element <- a){
      println(element)
      b :+ element
  }
  
  So the println works but they assignment doesn't.
  I am really frustated since it seems so easy and I do note get it. :(
  
  A:
  
  Let the Standard Library do the copying for you.
  val a : Array[String] = Array("foo","bar")
  val b : Array[String] = a.clone()
  
  The reason your code doesn't work is because :+ isn't an assignment operator. This statement, for example, Array() :+ "str" doesn't modify the Array, it creates a new Array with the modified value. Unfortunately your code doesn't assign that new Array to a new or existing variable so the modification is lost.
  You could do something like this ...
  for (element <- a){
    b = b :+ element
  }
  
  ... but there are better ways to get this done.
  update
  To filter string elements for multiple unrelated qualities you might try this.
  val acceptableRE = "(f.*|ba.*)".r
  val a: Array[String] = Array("foo", "bar", "buf", "bba")
  val b: Array[String] = a.collect{case acceptableRE(s) => s}   // Array(foo, bar)
  
  