Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using regex to access values from a map in keys

Tags:

regex

scala

val m = Map("a"->2,"ab"->3,"c"->4)

scala> m.get("a");


scala> println(res.get)
2

scala> m.get(/a\.*/)
// or something similar.

Can i get a list of all key-value pairs where key contains "a" without having to iterate over the entire map , by doing something as simple as specifying a regex in the key value?

Thanks in advance!

like image 784
joanOfArc Avatar asked Oct 29 '14 06:10

joanOfArc


1 Answers

No, you cannot do that without iterating over the entire map. In fact, I can't even think of a single data structure that would allow it, say nothing of the API.

Of course, iterating is pretty simple:

m.filterKeys(_ matches "a.*")
like image 52
Daniel C. Sobral Avatar answered Nov 10 '22 00:11

Daniel C. Sobral