How to simplify code block function

Hello everyone,

how can this code block function be further simplified:
e.g. fewer definitions str1, str2 …?

Best regards

Rolf

Rather than defining the search strings and then calling them in the function you could just define them in the function itself. You do still need to step through each replacement individually, since Dynamo won’t replace strings recursively like this.

Another option is to pull the string apart and then replace everything at once.

EDIT: You may also be able to simplify things even more with an Imperative block. I don’t have much experience here, so I’m not sure if you can make it any simpler or not.
image

7 Likes

Hi Nick,

thank you very much, this script look corresponds to my “aesthetic” ideas.

Thank you very much, this script look corresponds to my “aesthetic” ideas.

See you for my next problem then.

Best regards

Rolf

1 Like

Here is another option for you, courtesy of @JacobSmall and me playing :smiley:

def sets()
{
	return =
	    [
	        /*
	        A list of all your characters to replace
	        of any amount. Last character in list needs
	        to be the one you want the full list to
	        replace to in the form of:

	        [fromThis1, fromThis2, fromThisN, tothis]

	        */
	        ["à","á","â","ä","æ","ã","å","ā","a"],
	        ["ô","ö","ò","œ","ø","ō","õ","ó","o"]
	    ];
};

def altClean(str : string)
{
    characterSet = sets();
    str = [Imperative]
    {
		for (group in characterSet)
        {
			for (c in group)
            {
                s = str;
                s = String.Replace(s,c,group[-1]);
                str = s;
            }
        }
		return = str;
    }
	return = str;
};

definition_AltClean.dyn (5.3 KB)

3 Likes

Could you tell me some resources to learn design script language ?
Thanks alot.

Hi @Mo-Samir , here are a few resources :slight_smile:

  1. DesignScript Language Guide
  2. My DesignScript presentation at BILT EUR in Slovenia
  3. Dynamo Office Hours: DesignScript(ing) All the Things! recording
  4. Searching the forum for anything by @Vikram_Subbaiah who is a DesignScript genius :smiley:
6 Likes

This is the best source. :rofl:

7 Likes

Indeed! :smiley:

3 Likes

image

Shamelessly piling on to the Vikram hype.

5 Likes

Thank you @solamour
you are amazing :blush:

Thanks for being so kind and generous @solamour @Nick_Boyts @Robert_Younger

2 Likes

I was tring to do it with OOTB nodes splitting a text by the text to be replaced and adding in between the split list the replacements, but I am impressed of what I see posted here

Hi solamour,

unfortunately the script gives me “Null”.

What could be the cause?

Best regards

Rolf

A variant with Python (.Net)

import sys
import clr
import System

def altClean(txt):
	tempBytes = System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(txt)
	return System.Text.Encoding.UTF8.GetString(tempBytes)
	
OUT = altClean(IN[0])
2 Likes

If you’re in Manual mode, you may not have hit Run :slightly_smiling_face:

You could also try this associative alternative to @solamour’s code.

// Function
def alt(str:var[]..[],chr:var[]..[])
{
	r = List.LastItem(chr<1>);
	a = String.Replace(str<1>,chr,r);
	b = List.SetDifference(a,str);
	c = List.Flatten([str,b]);
	return List.LastItem(c);
};

// Characters
chr =
[["à","á","â","ä","æ","ã","å","ā","a"],
["ô","ö","ò","œ","ø","ō","õ","ó","o"]];

// Replacing
spl = String.Split(str,"");
con = String.Concat(alt(spl<1>,chr));
1 Like

Hi,

thanks for the other versions.

The problem with code block can be localized:

Warning: Multiple definitions for ‘String’ found as DSCore.String, List.String, Orchid.Common.String, WombatDynamo.String
Multiple definitions for ‘String’ found as DSCore.String, List.String, Orchid.Common.String, WombatDynamo.String
Multiple definitions for ‘String’ found as DSCore.String, List.String, Orchid.Common.String, WombatDynamo.String

But why do I need DSCore.String?

The Python variant is also very nice.

Best regards

Rolf

This is a Namespace problem.
In DesignScript you can call the standard (DSCore) (short for DesignScript) function for String.
In some Packages however, programmers used the same name for that function in their package.
What you have to do here is making sure you call the function embedded in that specific package.
So DSCore.String should be replaced by Wombat.String etc. or Orchid.String.
This is related to the old naming convention in older versions of that Package.
Updating the Package might help too.

2 Likes

Hi Marcel,

Thank you for the information.

All of my packages have been updated with Revit 22.

To be on the safe side, I always use DSCore \ Orchid …

Best regards

Rolf

1 Like