Search code examples
scalastring-interpolation

Scala string interpolation from collections - n number of variables


I want to interpolate a string pattern from a scala collection (Map, Seq, Hashtable) and populate a path to file.

${directory}/data/${fileName}

My collection is a Map[String,String] which holds directory and file values

args.directory and args.fileName

input from config file path_to_file: ${directory}/data/${fileName}

input from command args: directory=/temp,fileName=data.json

output: path_to_file = /temp/data/data.json

any suggestions?


Solution

  • If you have something like val args = Map("filename" -> "data.json", "directory" -> "temp"), then s"${args("directory")}/data/${args("filename")}" will evaluate to "/temp/data/data.json"