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?
scala string-interpolation
|
show 5 more comments
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?
scala string-interpolation
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
|
show 5 more comments
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?
scala string-interpolation
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
scala string-interpolation
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
|
show 5 more comments
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
|
show 5 more comments
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")}"
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
|
show 5 more comments
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"
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
add a comment |
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")}"
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
|
show 5 more comments
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")}"
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
|
show 5 more comments
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")}"
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")}"
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
|
show 5 more comments
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
|
show 5 more comments
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"
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
add a comment |
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"
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
add a comment |
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"
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"
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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