Merging multi-valued variables

Hi,
I’m trying to figure out how to join two multi-valued variables.
example:
_varA(A;B;C;D)
_varB(1;2;3;4)

result
_varC(A:1;B:2;C3;D4)

I tried with the $foreach function but I don’t understand how to match…

Assuming that you want to combine _varA and _varB consistently (which you have not done in your example – “A:1” and “B:2” contain colons while “C3” and “D4” don’t), you can do something like:

$set(_varC,)
$while($and($lt(%_loop_count%,$lenmulti(%_varA%)),$lt(%_loop_count%,$lenmulti(%_varB%))),
  $set(_idx,$sub(%_loop_count%,1))
  $set(_varC,%_varC%$getmulti(%_varA%,%_idx%):$getmulti(%_varB%,%_idx%); )
)
$setmulti(_varC,$trim(%_varC%,; ))

This will combine the elements of %_varA% and %_varB% into %_varC% (joined with a colon between the %_varA% and %_varB% values) until either %_varA% or %_varB% runs out of elements. For example, if %_varA% is “A; B; C; D” and %_varB% is “1; 2; 3; 4; 5” then %_varC% will be set to “A:1; B:2; C:3; D:4”. If you don’t want the colon separating the %_varA% and %_varB% values, simply remove it from Line 4 of the script.

EDIT: If you want it to continue combining all elements of both lists (using blanks for the missing elements in the shorter list), simply replace the $and with $or in Line 2 of the script. This way, if %_varA% is “A; B; C; D” and %_varB% is “1; 2; 3; 4; 5” then %_varC% will be set to “A:1; B:2; C:3; D:4; :5”.

3 Likes

Yes, my mistake. It should have been “C:3” and “D:4” :sweat_smile:

The script works perfectly, but some values of %_varB% are more “complicated” because they have multiple parts separated by commas and each part should be matched to the same value present in %_varA%

I’ll give you a concrete example:

%_varA%
Miro Vidovic;
Philip Vidovic;
Kirk Yano;
Roxanne Slimak;
Stephan Lupino;

%_varB%
[executive],[mix];
[co],[recording];
[executive];
[art direction],[design/illustration],[photography];
[mastering];

result
%_varC%
Miro Vidović:[executive];
Miro Vidović:[mix];
Philip Vidović:[co];
Philip Vidović:[recording];
Kirk Yano:[executive];
Roxanne Slimak:[art direction];
Roxanne Slimak:[design/illustration];
Roxanne Slimak:[photography];
Stephan Lupino:[mastering];

Is it possible to achieve this?

I believe so. Try the following:

$set(_varC,)
$while($and($lt(%_loop_count%,$lenmulti(%_varA%)),$lt(%_loop_count%,$lenmulti(%_varB%))),
  $set(_idx,$sub(%_loop_count%,1))
  $setmulti(_varB_multi,$getmulti(%_varB%,%_idx%),\,)
  $set(_save_loop_count,%_loop_count%)
  $set(_varA_value,$getmulti(%_varA%,%_idx%))
  $foreach(%_varB_multi%,
    $set(_varC,%_varC%%_varA_value%:$trim(%_loop_value%); )
  )
  $set(_loop_count,%_save_loop_count%)
)
$setmulti(_varC,$trim(%_varC%,; ))
1 Like

@Pianca82 please let me know if this revised version worked for you and did what you were asking. It’s the first time I’ve tried nesting loop functions.

1 Like

First of all, thanks @rdswift
I only had time to test it quickly in a file, but it seems to work properly.

In the field of roles I had 1 to 4 values that had to be matched for each artist and the script did it correctly.

I have to read the script carefully because I want to understand it well. I believe they are logics that can be useful as a basis for other scripts

Anyway ,in the next few days I should have more time to try it on a larger group of files. I will keep you up to date

1 Like

@rdswift after some more tests, I confirm that it works!

2 Likes

Excellent! Thanks for letting us know. Even though I wrote the code for the $foreach() and $while() functions, it wasn’t until your request that I even considered them being able to be nested.

2 Likes

sorry @rdswift but I still ask for your help.
Now that I have the final variable _varC I want to create new tags for certain values present in the _varC
Example:

$foreach(%_varC%,
  $if($in(%_loop_value%,mastering),$setmulti(mastering,$left(%_loop_value%,$find(%_loop_value%,:)); ),)
  )

everything is ok if I only have one artist with a “mastering” role. But if there are multiple artists with the mastering role, the new tag will always have one (I assume because each time it overwrites the previously saved value).
I tried to create a variable _idx with the value of the %_loop_count% and use it in various ways but without success

The problem is that you’re overwritng the value in %mastering% rather than appending to it. Try something like the following (untested code) and see if that helps:

$foreach(%_varC%,
  $if($in(%_loop_value%,mastering),$setmulti(mastering,%mastering%$left(%_loop_value%,$find(%_loop_value%,:)); ),)
  )

As written this may include an empty element at the end. If so, you might have to build the %mastering% tag initially as a normal string and after your loop strip any trailing semicolons and spaces, and then convert it to a multi-value.

2 Likes

It works! thank you again :wink:

1 Like