0

I have let's say 100 variables in a string , my requirement is to automatically create a Map out of the string:

String str = "$$test$$ $$test2$$ $$test$$ $$test3$$"  

Expected Result:

["test":test, "test2":test2, "test3":test3];

EDIT (for dsharew) This is the last version of my code

def list = queryText.findAll(/\$\$(.*?)\$\$/)

def map = [:]
list.each{
log.debug(it)
    it = it.replace("\$\$", "")
log.debug(it)
    map.putAt(it, it)
}

log.debug(list)
log.debug(map)
queryText = queryText.replaceAll(/\$\$(.*?)\$\$/) { k -> map[k[1]] ?: k[0] }
log.debug(queryText)

And the logs print the following result:

$$test$$
test
$$test2$$
test2
$$test$$
test
$$test3$$
test3
[$$test$$, $$test2$$, $$test$$, $$test3$$]
{test=test, test2=test2, test3=test3}
test test2 test test3
6
  • Do you really have hundred variables? what is your use case? Commented Apr 19, 2017 at 14:08
  • I have a script unit in Webratio which returns all the placeholders of hundreds of RTF templates and those are given as inputs in this other script which must do the mapping like I've shown Commented Apr 19, 2017 at 14:11
  • Does the variables have sequence its name? Commented Apr 19, 2017 at 14:22
  • Sorry I didn't understand what you asked, the variable always has the name of the placeholder, for example if I have $$NAME$$ in my template, I will need ["NAME":NAME] in my Map Commented Apr 19, 2017 at 14:28
  • have you tried my suggestion ? Commented Apr 19, 2017 at 14:32

3 Answers 3

1

This should do what you want:

def queryText = "\$\$test\$\$ \$\$test2\$\$ \$\$test\$\$ \$\$test3\$\$"
toMap(queryText.findAll(/\$\$(.*?)\$\$/));

def toMap(list){
    def map = [:]
    list.each{
        it = it.replace("\$\$", "")
        map.putAt(it, it)
    };
    println map;
    return map;

}
Sign up to request clarification or add additional context in comments.

14 Comments

My last step is the following: queryText = queryText.replaceAll(/\$\$(.*?)\$\$/) { k -> map[k[1]] ?: k[0] } so how can I adapt your solution?
what is this? queryText?
just a String such as "$$test1$$ contains test1"
ok can you post the final result of the value of queryText so I can change my solution for you
can you use different delimter because $ sign has a special meaning in groovy? can you use comma?
|
1

Following @dsharew answer, I've reduced it a little bit more:

​def queryText = "\$\$test\$\$ \$\$test2\$\$ \$\$test\$\$ \$\$test3\$\$"    

def resultMap = queryText
  .findAll(/\$\$(.*?)\$\$/)
  .collectEntries { String next ->         
    [next.replace("\$\$", "")] * 2                    
  } 
  • collectEntries can be used to return a map from a collection if it returns a map or a tuple for every entry in the collection.

  • If you multiply a list by n, you are creating a bigger list with n times its content

BTW cool problem!

Comments

0

This is what I came up with

String str = '$$test$$ $$test2$$ $$test$$ $$test3$$'
str.replaceAll('\\$\\$', '').split(' ').collectEntries { [(it):it] }

2 Comments

This code is ok, but doesn't work for example with: ​String str = '$$test$$ $$test2$$ $$test$$ $$test3$$ $$t' even if that's a very remote case. Thanks for your time anyways! May be helpful to somebody :)
You should add that as your example input and describe how partial words are handled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.