Find replace function

I agree, building a search-and-replace mechanism is a non-trivial task.
Among other things a solution would need to deal with …

  1. different levels within the code;
  2. mapping parameters;
  3. ambiguities as to which parts of the original code count as a “match”.

As a first baby step, we could build a F&R-function that will replace a single block with all parameters specified. Like:

Find and replace code script pic 2

Here’s my 1st attempt:

Implementation

Auxiliary function (working on lists, decomposed blocks / scripts)

https://snap.berkeley.edu/snap/snap.html#present:Username=qw23&ProjectName=Find%20and%20replace%20code

If @cookieclickerer33 (or anyone else) would like to expand it, please feel welcome to do so!

Just a "styling" issue with your blocks - for want of a better word :slight_smile:

When I was looking at the 1st block in your post, I got very confused about how it could possibly work as you seemed to be using a recursive function with no end case check

And then I got more confused as you seemed to have an identical named function (but with different type of parameters)

[ignore]And then there's a 3rd "identical" one :slight_smile:

I think changing the labels would make things a lot clearer :slight_smile:

You're right of course: the main function and the auxiliary function are identically named. I'm going to change the name of the second function (-> "HELPER ...").

The first image in my previous post (#21) is a "result pic" of the main function.The second image shows its implementation. The final image shows the implementation of the auxiliary (= second) function. There never was a third function. Or perhaps I've been missing a joke ( :slight_smile: )? :wink:

Seriously: even though @cookieclickerer33 appears to consider her original question answered I'm convinced the function could be further developed so as to cover more cases. Follow the link in my previous post (#21) and you'll see what I mean. If you have any suggestions I'd be happy to receive those.

i'd just got totally confused - it was early here :slight_smile:

I've been having to do a bit of script editing (for my 3D OpenSCAD project)

I already had deep list search and replace blocks, so I've been using them but might switch to your approach instead :slight_smile:

One thing to consider, (as its something I'm doing) is doing a wider search/replace on all scripts in a sprite (or even all scripts in a project)

I made some updates to my Find & Replace project just now.

updates
  • changed the name of the auxiliary function;
  • expanded the scope to replacing numbers, text, variables etc.;
  • added some to-do cases.

Find and replace code script pic
Find and replace code script pic (1)
Find and replace code script pic (2)

Could you share some relevant code from your 3D OpenSCAD project?
(and I don't mean the entire project :slight_smile: )

Not sure if it'll make sense out of the context of the project but here is a bit where I replace a command block with the result of a call to a reporter

OpenSCAD_3_5_0_dev script pic

Thanks for sharing. However, it looks quite complicated!

OK, here's one of my hobbyhorses :wink: In my limited experience ... code becomes more readable (and therefore: portable, maintainable etc.) if intermediate results are kept in variables with relevant names.

Example

... is so much more readable than:

Programming tools SANDBOX script pic (27)

(the first version is also faster; I haven't bothered to find out why)

TLDR: could you rewrite your (admittedly: impressive) piece of code in a more readable style? :slight_smile:

I could but I won't be - I'm still developing the project and this code could be thrown away at any point :slight_smile:

And I'm not into tidying up internal code - I only tidy up / optimise stuff if publishing a custom block :slight_smile:

Ok, that's up to you of course.

Would this one be better?


(Mock-up)
(Also the “all” should be before the first ring)

qw23's new version looks fine now that they've renamed one of the reporters

I also recommend changing the name of the “identity property “ reporter to just be “text []” or so that it’s more readable and easier to grab in the pallet

Do you mean the orange identity circle thingy in ... ?

Find and replace code script pic (1)

That's not part of the function, it's just a test and demonstration script.
But it works with other (Snap! primitive) operators as well, provided there's no grey ring around it:

Find and replace code script pic (6)

Yea I ment that, it would be good to make it or make an entirely new block that has a text input in it

For me it depends on the number of intermediate results and the simplicity of the algorithm. A middle ground is the PIPE block.

As a teacher, rather than as a programmer, I want to be sure students don't think you have to put every intermediate result in a variable, so I think it's important to have some nested function call examples.

But, you know, in your code, knock yourself out!

I agree to some extent - yet I feel that many (experienced) programmers - not excluding myself - tend to overuse composition, underestimating the self-documenting power of making intermediate results explicit.

I made another update to my my Find & Replace project for Snap ! code.
Now it can also handle block sequences. I found it especially challenging to replace a single block by a sequence of blocks, or vice versa.

This is an example of what it can do
Find and replace code script pic (13)

This is the palette function, decomposing the input data and reassembling the output

And finally the recursive HELPER function doing (of course) most of the hard work
(click to enlarge)

What I haven't achieved yet is a function that can also handle wildcards - perhaps too difficult for me as an programming amateur :smirk:

Nice work.

Do you mean, e.g., untitled script pic (2)? Or untitled script pic (3)? There's lots of specifying to do before you start coding!