Loop over code to avoid code repetitions

#1

Hi,

In my code I have a lot of repetitions of almost the same code. Now I just copy them, but I was wondering if in AnyBody there is a possibility to write some kind of loop over the code in order to define all these objects in a few lines of code. I have been searching for it in the reference manual, but can’t seem to find something that could do this. Below an example of what I mean with the repetitions:

AnyRefNode F_pMCL00 = {
sRel = …Landmarks.F_pMCL00;
ARel = Main.Model.Seg.Thigh.STLpoint.FemurMechAxis.ARel;};
AnyRefNode F_pMCL01 = {
sRel = …Landmarks.F_pMCL01;
ARel = Main.Model.Seg.Thigh.STLpoint.FemurMechAxis.ARel;};
AnyRefNode F_pMCL02 = {
sRel = …Landmarks.F_pMCL02;
ARel = Main.Model.Seg.Thigh.STLpoint.FemurMechAxis.ARel;};

Thanks!

Kind regards,

Laura

#2

Hi Laura

That is a good question. AnyScript doesn’t support that kind of syntax. It is not an Imperativ programming language. Unfortunately.

There are options to encapsulate and reuse code (e.g. class_template’s) but it doesn’t solve your problem here.

I see two options:

  1. Write a small script that generates your AnyScript code. This could be in Matlab or Python. I have often used the Jinja2 Python library for this task.
  2. Colapse your code to a single line and then use an editor like Notepad++ or VS Code to quickly create your code.

Here I used the Increment selection extension for VSCode. (and AnyScript extension for syntax highlighting). But Notepad++ has the same feature built-in (Press Alt-C to activate the column editor)

BW
Morten