I was trying to replace a string within a list of strings
var keys=List("a.SITE", "a.SITEID")
var column_expr="coalesce(nullif(SITENAME ,''),'UNKNOWN')"
keys = keys.map(element=>element.replace("SITEID",column_expr))
println(keys)
which prints as:
List(a.SITE, a.coalesce(nullif(SITENAME ,''),'UNKNOWN'))
I was expecting something like below:
List(a.SITE, coalesce(nullif(SITENAME ,''),'UNKNOWN'))
I don't want alias 'a' to be present in the final list. It can have any character. like a,b or c etc.
FYI..
Other part of logic prevents me to pass it as a.SITEID
. I can only pass the column name.
Following may be what you are looking for:
val keys = List("a.SITE", "a.SITEID")
val columnExpr = "coalesce(nullif(SITENAME ,''),'UNKNOWN')"
val updatedKeys = keys.map(_.replaceAll(".*\\.SITEID", columnExpr))
println(updatedKeys)
//List(a.SITE, coalesce(nullif(SITENAME ,''),'UNKNOWN'))
The replaceAll(".*\\.", "")
removes everything before and including the last dot. So please make sure that is what you need. Also, I have changed mutable variables to immutable ones to make them more functional.