Help with script and $foreach loop

I have been trying to figure out why this is not working for about 4 hours. I must be missing something.

Any help is appreciated.

$set(_ScriptVer, .016)

$noop(Required tags Library. Next line sets up the default values to be used when a required tag is missing)
$setmulti(_ReqTags,splfnversion; tracknumber; title; artist; album; version)
$setmulti(_ReqDefaults,%_ScriptVer%; 1; Unknown; Unknown; Unknown; 1)

$noop(------------------MAIN SCRIPT--------------)
$noop(
	Loop through each entry in the _ReqTags variable, checking to see if that tag exists in the current file and has a value.
)
$foreach(_ReqTags,
     $noop(
     1. Check for the existence of the selected _RegTags entry.
     2. If the tag does not exist in the file, then we create it and set the value to the corresponding entry
        in the multi _ReqDefaults.
     
     )
	
     $if($not($get(%_loop_value%)),
          $set(%_loop_value%,$getmulti(%_ReqDefaults%,%_loop_count%))
     ,

     )
)

I am guessing here, but perhaps the problem is because the %_loop_count% value starts at 1 and the $getmulti() index starts at 0.

3 Likes

I forgot that. Adjust and test.

Thanks

You’re a genius.

Thank you again.

1 Like

Well your reply fixed one issue.

I’ve modified the code to incorporate what you said, and some other cosmetic changes that shouldn’t affect anything. The problem still exists where the foreach does not run through each value. All I end up with is one tag of “temp0” wet to a value of “_ReqTagDefaults”. I can not figure out why its not iterating through each value and creating a “temp#” tag set to the corresponding value in the second multivalue variable.

The code is as follows:

$set(_ScriptVer,.17)

$setmulti(_ReqTags,splfnversion; tracknumber; title; artist; album; version)
$setmulti(_ReqTagDefaults,%_ScriptVer%; 1; Unknown; Unknown; Unknown; 1)

$foreach(_ReqTags,
     $set(_idx,%_loop_count%)
     $set(temp$sub(%_idx%,1),$getmulti(_ReqTagDefaults,$sub(%_idx%,1)))
)

I’ve lost hours trying to figure this out. The towels are just about thrown in.

Any help appreciated

Two things…

First, you need to pass the multi-value variable to the $foreach() function enclosed within percent signs as %_ReqTags%. By leaving out the percent signs, it was being interpreted as a single variable with the value “_ReqTags”.

Second, for some reason that I haven’t identified yet, the $foreach() function is having trouble parsing the multi-value variables within the code parameter. To get it to work, I had to force the $getmulti() functions to re-split the value by enclosing the variable name with percent signs as %_ReqTagDefaults% and include the third parameter "; ".

I rewote your test script to the following, and it seems to work.

$set(_ScriptVer,.17)

$setmulti(_ReqTags,splfnversion; test_tracknumber; test_title; test_artist; test_album; test_version)
$setmulti(_ReqTagDefaults,%_ScriptVer%; 1; Unknown; Unknown; Unknown; 1)

$foreach(%_ReqTags%,
     $set(_idx,%_loop_count%)
     $set(temp_$sub(%_loop_count%,1),$getmulti(%_ReqTagDefaults%,$sub(%_loop_count%,1),; ))
)

I think this might work to achieve what you wanted:

$set(_ScriptVer,.17)

$setmulti(_ReqTags,splfnversion; tracknumber; title; artist; album; version)
$setmulti(_ReqTagDefaults,%_ScriptVer%; 1; Unknown; Unknown; Unknown; 1)

$foreach(%_ReqTags%,
       $if($not($get(%_loop_value%)),$set(%_loop_value%,$getmulti(%_ReqTagDefaults%,$sub(%_loop_count%,1),; )))
)
3 Likes

Once again, Thank you. I see you ran into the same issue I was having about the parsing. I just could not figure out why it wasn’t going through the values.

If you lived closer, I would buy you a drink.

Take care

2 Likes

This was an interesting issue, and I’m glad that you brought it up. Now that we understand that there is a parsing issue, I’ll add a note to the $foreach() documentation describing the issue and the work-around that we’ve come up with to get it to work. That way it might save others from going through the same pain that you experienced.

2 Likes

I should really do more checking before I put my foot in my mouth.

It turns out that there is no parsing issue with the loop commands. The only issue in this case was that the multi-value variable name being passed to the $getmulti() command wasn’t enclosed within percent signs, so it was being interpreted as a literal.

Also, after checking I found that specifying the separator as "; " wasn’t necessary either.

No changes to the documentation required. Sorry about the confusion.

4 Likes

Thanks for the clarification. I have a suggestion then, since it seems that the use of the % is optional sometimes when referring to variable/tag, to avoid confusion and situations like this, why not make the use of the % mandatory/required at ALL times?

That way someone doesn’t have to refer to the documentation to see it the wrapping in % s is necessary.

I think it would bring more consistency and ease of scripting.

Just my thoughts

The percentage signs as in %var% are essentially a short notation for writing $get(var). With both notations you get the value of the variable specified.

Some functions expect the variable name as they need to perform something on that variable directly. E.g. $delete(var) expects the name, as it will both clear that variable and mark the tag of that name for deletion.

2 Likes