String.Replace for multiple items


#1

Hi All,

This is my first post, so I am hoping to get some good feedback and be an active part of the dynamo community. I am working on a project that requires us to use different specification numbers for Keynoting than what is typically our standard. This means that we will need to change all of the detail components that we are keynoting to have the new numbers. This seems like a great exercise for Dynamo, so I’ll share my progress and hope you guys can help me through the rest.

I tested the concept by grabbing all the keynote tags in the project, getting the “Key Value” parameter, and using the String.Replace node to replace the number. I then used the Element.SetParameterByName node to write the new number back to the project. See image 01.

[caption id=“attachment_30002” align=“alignnone” width=“600”]ReplaceKeynotes-01 Image 01[/caption]

Next, I would like to use excel to create two lists side-by-side. (old spec numbers and new spec numbers) I then want to use those lists to search for a string, and if found, replace that string with the other list. I just can’t seem to map the function correctly. See image 02.

[caption id=“attachment_30003” align=“alignnone” width=“600”]ReplaceKeynotes-02 Image 02[/caption]

Any ideas? Thank so much for the help.


#2

Hello Chris,

The “List.Combine” node is matching up an item from each list with the equivalent position item in the other lists. Because your 2nd and 3rd input lists have only two items, item three and beyond from the first list don’t have anything to be paired up with, the “String.Replace” node fails and you end up with null values.

You could try to use another list combination node like “List.LaceShortest/Longest” or use your key values list as an input in the function. However that will either give you an incomplete result or a separate result for each replaced value:

2015-09-20_21-11-46

What you really need to do is replace the values recursively. That means that after each successful match and replacement, the resulting list is used for the next replacement. The best way to do this is to either use an imperative DesignScript function or a python script, which would over-complicate things. What I would suggest instead, is to use a neat little node from the Clockwork package, called “List.ReplaceItemAtIndex”. It sounds very similar to the built-in node with the same name but the difference is that clockwork’s node can replace recursively:

2015-09-20_21-11-56

We only need to find the indices of the items that we want to replace and we can do that quite easily with “IndexOf” + “List.Map”. Try it out and tell me if it works for you.


#3

File: StringReplace.dyn

20150920-3


#4

Seems like Dimitar and me replied at about the same time.

What I’ve posted is the alternate Design Script approach that Dimitar referred to in his reply.


#5

Thank you both for the helpful responses! It’s nice to see the thought process and the different approaches to solve the problem. I got it working using the Design Script approach. This will save lots of time on the project.

Again. Thanks to both of you.