$loop() for dummies

I'm not a programmer, but I find the export configuration function in mp3tag to be quite powerful for maintaining and reporting on my podcast collection, and so I muddled through. What I've found through trial-and-error is you should think about the $loop more as a sort function. This is not based on any real knowledge, rather on my reading of the output from many dozens of my hand-built export configuration files.

The $loop parameter, the %album% in $loop(%album%), sorts your mp3tag listing before executing your export code. For example, $loop(%album%) sorts your mp3tag selection by album, then executes your export code line by line on the results.

$loop(%dummy%) would execute your code based on your last manual sorting operation. $loop(%album%)$loop(%title%) sorts your mp3tag listing first by title, then by album, and then executes your code (note the reverse order).

$loop()$loop()$loopend()$loopend() does NOT execute your code twice. It merely sorts twice then executes your code once. To execute code twice use $loop()...coding...$loopend() $loop()...coding...$loopend().

If you have an export configuration that should execute differently depending on how the mp3tag listing is sorted, then you should use %dummy% as the $loop parameter. For example, I use an export configuration to print out playlists. For podcasts that need to be listened to in order, I first sort manually by track and then run the export configuration. For podcasts that can be listened to in any order, I first sort manually by random.

This at least is how I see it. I welcome any clarifications.

Did you know that the File menu has an extra function to generate playlists from the currently displayed lists (and selected files). So you don't have to create a separate report for playlists.

You did not mention the
$loop(%fieldname%,1) which sorts but outputs each item only once. This might be useful if you want to count certain occurrances. Or find similar but not totally equal entries.

One can perhaps say so.

Another interpretation may be, that the first $loop() sorts by the value %ALBUM%, ...
and within this sorted workspace, ...
then the second $loop() sorts by the value %TITLE% within each related ALBUM.

If you apply $loop(%DUMMY%), then Mp3tag takes the current sequence of the selected files in the listview as the input.

If you apply $loop(%ALBUM%%TITLE%), then this may give other results than a nested $loop().


Yes this is curious. There's a gap in my understanding of loops ... neither $loop(%album%%title%) nor $loop(%title%%album%) give the same result (sometimes) as $loop(%album%)$loop(%title%).

I'll think about this ...


Variables inside the loop are different (i.e. have different scope) from the ones outside the loop.
This simple code sets a variable before the loop and then changes its value inside the loop. Added second loop for better understanding. Run the export for few selected files.

$loop(%album%)value inside loop=$put(val,$add($get(val),1))$loopend()
$loop(%album%)value inside loop=$put(val,$add($get(val),1))$loopend()

Someone would expect values to incremental in all script. Not exactly.
Output for 5 selected files would be:

value inside loop=1
value inside loop=2
value inside loop=3
value inside loop=4
value inside loop=5
value inside loop=6
value inside loop=7
value inside loop=8
value inside loop=9
value inside loop=10
Interesting. I find it odd but it is what it is.
It all depends on the design (intention).
edit: fixed typo ($endloop --> $loopend)