Scala string interpolation from collections - n number of variables











up vote
0
down vote

favorite












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?










share|improve this question
























  • use it like this s"${directory}/data/${fileName}" it should work
    – Raman Mishra
    Nov 14 at 17:33










  • and the map should be like Map[String, List[String]] as value can me more than one file in a directory?
    – Raman Mishra
    Nov 14 at 17:35










  • my interpolated (to be) values come as a Map Collection and for the interpolation pattern that you have offered you need declared vars in the scope of the s (sugar function)
    – Rio Amarillo
    Nov 14 at 17:37












  • it's not necessary that you declare a var you can use ListBuffer
    – Raman Mishra
    Nov 14 at 17:39










  • example por favor
    – Rio Amarillo
    Nov 14 at 17:39















up vote
0
down vote

favorite












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?










share|improve this question
























  • use it like this s"${directory}/data/${fileName}" it should work
    – Raman Mishra
    Nov 14 at 17:33










  • and the map should be like Map[String, List[String]] as value can me more than one file in a directory?
    – Raman Mishra
    Nov 14 at 17:35










  • my interpolated (to be) values come as a Map Collection and for the interpolation pattern that you have offered you need declared vars in the scope of the s (sugar function)
    – Rio Amarillo
    Nov 14 at 17:37












  • it's not necessary that you declare a var you can use ListBuffer
    – Raman Mishra
    Nov 14 at 17:39










  • example por favor
    – Rio Amarillo
    Nov 14 at 17:39













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question















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?







scala string-interpolation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 17:50

























asked Nov 14 at 17:27









Rio Amarillo

32




32












  • use it like this s"${directory}/data/${fileName}" it should work
    – Raman Mishra
    Nov 14 at 17:33










  • and the map should be like Map[String, List[String]] as value can me more than one file in a directory?
    – Raman Mishra
    Nov 14 at 17:35










  • my interpolated (to be) values come as a Map Collection and for the interpolation pattern that you have offered you need declared vars in the scope of the s (sugar function)
    – Rio Amarillo
    Nov 14 at 17:37












  • it's not necessary that you declare a var you can use ListBuffer
    – Raman Mishra
    Nov 14 at 17:39










  • example por favor
    – Rio Amarillo
    Nov 14 at 17:39


















  • use it like this s"${directory}/data/${fileName}" it should work
    – Raman Mishra
    Nov 14 at 17:33










  • and the map should be like Map[String, List[String]] as value can me more than one file in a directory?
    – Raman Mishra
    Nov 14 at 17:35










  • my interpolated (to be) values come as a Map Collection and for the interpolation pattern that you have offered you need declared vars in the scope of the s (sugar function)
    – Rio Amarillo
    Nov 14 at 17:37












  • it's not necessary that you declare a var you can use ListBuffer
    – Raman Mishra
    Nov 14 at 17:39










  • example por favor
    – Rio Amarillo
    Nov 14 at 17:39
















use it like this s"${directory}/data/${fileName}" it should work
– Raman Mishra
Nov 14 at 17:33




use it like this s"${directory}/data/${fileName}" it should work
– Raman Mishra
Nov 14 at 17:33












and the map should be like Map[String, List[String]] as value can me more than one file in a directory?
– Raman Mishra
Nov 14 at 17:35




and the map should be like Map[String, List[String]] as value can me more than one file in a directory?
– Raman Mishra
Nov 14 at 17:35












my interpolated (to be) values come as a Map Collection and for the interpolation pattern that you have offered you need declared vars in the scope of the s (sugar function)
– Rio Amarillo
Nov 14 at 17:37






my interpolated (to be) values come as a Map Collection and for the interpolation pattern that you have offered you need declared vars in the scope of the s (sugar function)
– Rio Amarillo
Nov 14 at 17:37














it's not necessary that you declare a var you can use ListBuffer
– Raman Mishra
Nov 14 at 17:39




it's not necessary that you declare a var you can use ListBuffer
– Raman Mishra
Nov 14 at 17:39












example por favor
– Rio Amarillo
Nov 14 at 17:39




example por favor
– Rio Amarillo
Nov 14 at 17:39












2 Answers
2






active

oldest

votes

















up vote
-1
down vote



accepted










As you have described that you have Map[String,String]



val args:Map[String, String] = ("dir1" -> "file1","dir2"-> "file2")

val result = args.map{ keyValue =>
s"${keyValue._1}/data/${keyValue._2}"


}



and result will have all the paths.



as Dima suggested if you have something like



Map("directory"->"temp", "fileName"->"data.json")


then you can do it like this:



val path = s"${args("directory")}/data/${args("fileName")}"





share|improve this answer























  • You don't need mutable collections for doing stuff like this. args.map { case (k,v) => s"$k/data/$v" } will do.
    – Dima
    Nov 14 at 20:03










  • I am considering map has more than one value @Dima that's why i am using mutable.ListBuffer yes if it has only 2 keys we don't need it I agree. But it doesn't looks like he has only two keys!
    – Raman Mishra
    Nov 15 at 5:06










  • it doesn't matter how many values. The snippet I showed above works for any number of them.
    – Dima
    Nov 15 at 11:49










  • what if there is more keys? will it work for them too? i don't think so @Dima we will have to iterate over then in that condition and then will save paths for each key value. Suppose it has 2 values for directory -> List("temp", "temp1") so your code snippet will print "/List(temp, temp1)/data/data.json".
    – Raman Mishra
    Nov 15 at 11:53










  • Yes, this is written to fit the example in your your answer. If values are lists, your code doesn't work too. You'd have to write it differently then ... but still don't need mutable containers.
    – Dima
    Nov 15 at 11:56




















up vote
0
down vote













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"






share|improve this answer





















  • answer will hold only if it has only two keys!! but the idea is right i have updated my answer thank you!!
    – Raman Mishra
    Nov 14 at 18:40










  • Well, yeah ... that's what he said: my collection looks like this: [fileName, 'someVal'] [Directory, 'someVal'] So ... two keys.
    – Dima
    Nov 14 at 19:59













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305726%2fscala-string-interpolation-from-collections-n-number-of-variables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
-1
down vote



accepted










As you have described that you have Map[String,String]



val args:Map[String, String] = ("dir1" -> "file1","dir2"-> "file2")

val result = args.map{ keyValue =>
s"${keyValue._1}/data/${keyValue._2}"


}



and result will have all the paths.



as Dima suggested if you have something like



Map("directory"->"temp", "fileName"->"data.json")


then you can do it like this:



val path = s"${args("directory")}/data/${args("fileName")}"





share|improve this answer























  • You don't need mutable collections for doing stuff like this. args.map { case (k,v) => s"$k/data/$v" } will do.
    – Dima
    Nov 14 at 20:03










  • I am considering map has more than one value @Dima that's why i am using mutable.ListBuffer yes if it has only 2 keys we don't need it I agree. But it doesn't looks like he has only two keys!
    – Raman Mishra
    Nov 15 at 5:06










  • it doesn't matter how many values. The snippet I showed above works for any number of them.
    – Dima
    Nov 15 at 11:49










  • what if there is more keys? will it work for them too? i don't think so @Dima we will have to iterate over then in that condition and then will save paths for each key value. Suppose it has 2 values for directory -> List("temp", "temp1") so your code snippet will print "/List(temp, temp1)/data/data.json".
    – Raman Mishra
    Nov 15 at 11:53










  • Yes, this is written to fit the example in your your answer. If values are lists, your code doesn't work too. You'd have to write it differently then ... but still don't need mutable containers.
    – Dima
    Nov 15 at 11:56

















up vote
-1
down vote



accepted










As you have described that you have Map[String,String]



val args:Map[String, String] = ("dir1" -> "file1","dir2"-> "file2")

val result = args.map{ keyValue =>
s"${keyValue._1}/data/${keyValue._2}"


}



and result will have all the paths.



as Dima suggested if you have something like



Map("directory"->"temp", "fileName"->"data.json")


then you can do it like this:



val path = s"${args("directory")}/data/${args("fileName")}"





share|improve this answer























  • You don't need mutable collections for doing stuff like this. args.map { case (k,v) => s"$k/data/$v" } will do.
    – Dima
    Nov 14 at 20:03










  • I am considering map has more than one value @Dima that's why i am using mutable.ListBuffer yes if it has only 2 keys we don't need it I agree. But it doesn't looks like he has only two keys!
    – Raman Mishra
    Nov 15 at 5:06










  • it doesn't matter how many values. The snippet I showed above works for any number of them.
    – Dima
    Nov 15 at 11:49










  • what if there is more keys? will it work for them too? i don't think so @Dima we will have to iterate over then in that condition and then will save paths for each key value. Suppose it has 2 values for directory -> List("temp", "temp1") so your code snippet will print "/List(temp, temp1)/data/data.json".
    – Raman Mishra
    Nov 15 at 11:53










  • Yes, this is written to fit the example in your your answer. If values are lists, your code doesn't work too. You'd have to write it differently then ... but still don't need mutable containers.
    – Dima
    Nov 15 at 11:56















up vote
-1
down vote



accepted







up vote
-1
down vote



accepted






As you have described that you have Map[String,String]



val args:Map[String, String] = ("dir1" -> "file1","dir2"-> "file2")

val result = args.map{ keyValue =>
s"${keyValue._1}/data/${keyValue._2}"


}



and result will have all the paths.



as Dima suggested if you have something like



Map("directory"->"temp", "fileName"->"data.json")


then you can do it like this:



val path = s"${args("directory")}/data/${args("fileName")}"





share|improve this answer














As you have described that you have Map[String,String]



val args:Map[String, String] = ("dir1" -> "file1","dir2"-> "file2")

val result = args.map{ keyValue =>
s"${keyValue._1}/data/${keyValue._2}"


}



and result will have all the paths.



as Dima suggested if you have something like



Map("directory"->"temp", "fileName"->"data.json")


then you can do it like this:



val path = s"${args("directory")}/data/${args("fileName")}"






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 at 12:23

























answered Nov 14 at 17:54









Raman Mishra

1,0231416




1,0231416












  • You don't need mutable collections for doing stuff like this. args.map { case (k,v) => s"$k/data/$v" } will do.
    – Dima
    Nov 14 at 20:03










  • I am considering map has more than one value @Dima that's why i am using mutable.ListBuffer yes if it has only 2 keys we don't need it I agree. But it doesn't looks like he has only two keys!
    – Raman Mishra
    Nov 15 at 5:06










  • it doesn't matter how many values. The snippet I showed above works for any number of them.
    – Dima
    Nov 15 at 11:49










  • what if there is more keys? will it work for them too? i don't think so @Dima we will have to iterate over then in that condition and then will save paths for each key value. Suppose it has 2 values for directory -> List("temp", "temp1") so your code snippet will print "/List(temp, temp1)/data/data.json".
    – Raman Mishra
    Nov 15 at 11:53










  • Yes, this is written to fit the example in your your answer. If values are lists, your code doesn't work too. You'd have to write it differently then ... but still don't need mutable containers.
    – Dima
    Nov 15 at 11:56




















  • You don't need mutable collections for doing stuff like this. args.map { case (k,v) => s"$k/data/$v" } will do.
    – Dima
    Nov 14 at 20:03










  • I am considering map has more than one value @Dima that's why i am using mutable.ListBuffer yes if it has only 2 keys we don't need it I agree. But it doesn't looks like he has only two keys!
    – Raman Mishra
    Nov 15 at 5:06










  • it doesn't matter how many values. The snippet I showed above works for any number of them.
    – Dima
    Nov 15 at 11:49










  • what if there is more keys? will it work for them too? i don't think so @Dima we will have to iterate over then in that condition and then will save paths for each key value. Suppose it has 2 values for directory -> List("temp", "temp1") so your code snippet will print "/List(temp, temp1)/data/data.json".
    – Raman Mishra
    Nov 15 at 11:53










  • Yes, this is written to fit the example in your your answer. If values are lists, your code doesn't work too. You'd have to write it differently then ... but still don't need mutable containers.
    – Dima
    Nov 15 at 11:56


















You don't need mutable collections for doing stuff like this. args.map { case (k,v) => s"$k/data/$v" } will do.
– Dima
Nov 14 at 20:03




You don't need mutable collections for doing stuff like this. args.map { case (k,v) => s"$k/data/$v" } will do.
– Dima
Nov 14 at 20:03












I am considering map has more than one value @Dima that's why i am using mutable.ListBuffer yes if it has only 2 keys we don't need it I agree. But it doesn't looks like he has only two keys!
– Raman Mishra
Nov 15 at 5:06




I am considering map has more than one value @Dima that's why i am using mutable.ListBuffer yes if it has only 2 keys we don't need it I agree. But it doesn't looks like he has only two keys!
– Raman Mishra
Nov 15 at 5:06












it doesn't matter how many values. The snippet I showed above works for any number of them.
– Dima
Nov 15 at 11:49




it doesn't matter how many values. The snippet I showed above works for any number of them.
– Dima
Nov 15 at 11:49












what if there is more keys? will it work for them too? i don't think so @Dima we will have to iterate over then in that condition and then will save paths for each key value. Suppose it has 2 values for directory -> List("temp", "temp1") so your code snippet will print "/List(temp, temp1)/data/data.json".
– Raman Mishra
Nov 15 at 11:53




what if there is more keys? will it work for them too? i don't think so @Dima we will have to iterate over then in that condition and then will save paths for each key value. Suppose it has 2 values for directory -> List("temp", "temp1") so your code snippet will print "/List(temp, temp1)/data/data.json".
– Raman Mishra
Nov 15 at 11:53












Yes, this is written to fit the example in your your answer. If values are lists, your code doesn't work too. You'd have to write it differently then ... but still don't need mutable containers.
– Dima
Nov 15 at 11:56






Yes, this is written to fit the example in your your answer. If values are lists, your code doesn't work too. You'd have to write it differently then ... but still don't need mutable containers.
– Dima
Nov 15 at 11:56














up vote
0
down vote













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"






share|improve this answer





















  • answer will hold only if it has only two keys!! but the idea is right i have updated my answer thank you!!
    – Raman Mishra
    Nov 14 at 18:40










  • Well, yeah ... that's what he said: my collection looks like this: [fileName, 'someVal'] [Directory, 'someVal'] So ... two keys.
    – Dima
    Nov 14 at 19:59

















up vote
0
down vote













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"






share|improve this answer





















  • answer will hold only if it has only two keys!! but the idea is right i have updated my answer thank you!!
    – Raman Mishra
    Nov 14 at 18:40










  • Well, yeah ... that's what he said: my collection looks like this: [fileName, 'someVal'] [Directory, 'someVal'] So ... two keys.
    – Dima
    Nov 14 at 19:59















up vote
0
down vote










up vote
0
down vote









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"






share|improve this answer












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"







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 at 18:10









Dima

23.6k32234




23.6k32234












  • answer will hold only if it has only two keys!! but the idea is right i have updated my answer thank you!!
    – Raman Mishra
    Nov 14 at 18:40










  • Well, yeah ... that's what he said: my collection looks like this: [fileName, 'someVal'] [Directory, 'someVal'] So ... two keys.
    – Dima
    Nov 14 at 19:59




















  • answer will hold only if it has only two keys!! but the idea is right i have updated my answer thank you!!
    – Raman Mishra
    Nov 14 at 18:40










  • Well, yeah ... that's what he said: my collection looks like this: [fileName, 'someVal'] [Directory, 'someVal'] So ... two keys.
    – Dima
    Nov 14 at 19:59


















answer will hold only if it has only two keys!! but the idea is right i have updated my answer thank you!!
– Raman Mishra
Nov 14 at 18:40




answer will hold only if it has only two keys!! but the idea is right i have updated my answer thank you!!
– Raman Mishra
Nov 14 at 18:40












Well, yeah ... that's what he said: my collection looks like this: [fileName, 'someVal'] [Directory, 'someVal'] So ... two keys.
– Dima
Nov 14 at 19:59






Well, yeah ... that's what he said: my collection looks like this: [fileName, 'someVal'] [Directory, 'someVal'] So ... two keys.
– Dima
Nov 14 at 19:59




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305726%2fscala-string-interpolation-from-collections-n-number-of-variables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?