# How to detect the last iteration in a loop?

**URL:** <https://community.mp3tag.de/t/how-to-detect-the-last-iteration-in-a-loop/55543>\
**Category:** Web Sources Discussion\
**Created:** [December 16, 2021, 12:54pm UTC](https://community.mp3tag.de/t/how-to-detect-the-last-iteration-in-a-loop/55543 "2021-12-16T12:54:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)\
**Post date:** [December 16, 2021, 12:54pm UTC](https://community.mp3tag.de/t/how-to-detect-the-last-iteration-in-a-loop/55543/1 "2021-12-16T12:54:05Z")

</div>

I would like to add multiple values to the same variable in a loop (separated by a slash) from this JSON data:

![image](https://community.mp3tag.de/uploads/default/original/2X/1/198e1d55751b992bc5c80b471e90308a269a1c49.png)

Currently, I know 2 ways to do that:

1.) Using `json_select_many` which works perfectly fine for the `catalog-number`

```auto
json_select_object "label-info"
    outputto "CATALOGNUMBER"
    json_select_many "label-info" "catalog-number" " / "
    sayrest
json_unselect_object

```

and results in the expected  
`LR-1893008 / LR-1893008`  
**Unfortunately, I don't get this working for the disambiguations**

2.) Using `json_foreach` & `json_select` in the example for the PUBLISHER where I want to add the disambiguation in parenthesis

```auto
outputto "PUBLISHER"
    json_foreach "label-info"
        json_select "label"
        json_select "name"
            sayrest
        json_select "disambiguation"
        ifnot ""
            say "("
            sayrest
            say ")"
            say " / " # Always add a Space and Slash and Space at the end of the output
        endif
    json_foreach_end

```

In this case, the PUBLISHER content always ends with a "` / ` ".  
Because  
a) I don't know an indicator when the last iteration in the loop appears  
b) I also don't know how I could increase a counter by +1 for every iteration  
and  
c) I don't get the max number of iterations (also known as max number of objects/elements) which I could compare to the current iteration

How do you solve this?

---

<div class="post-metadata">

**Author:** ![Romano](https://community.mp3tag.de/user_avatar/community.mp3tag.de/romano/32/7465_2.png) [@Romano](https://community.mp3tag.de/u/Romano)\
**Post date:** [December 16, 2021, 8:04pm UTC](https://community.mp3tag.de/t/how-to-detect-the-last-iteration-in-a-loop/55543/2 "2021-12-16T20:04:41Z")

</div>

> [@LyricsLover](#):
>
> In this case, the PUBLISHER content always ends with a "`/` ".

i only made one json version script, but maybe if something like this:

![](https://community.mp3tag.de/uploads/default/original/2X/a/ad4464d5997869d4e2a105ca51c036b0c7e24045.png)

can you show the link for the json?

---

<div class="post-metadata">

**Author:** ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)\
**Post date:** [December 16, 2021, 8:44pm UTC](https://community.mp3tag.de/t/how-to-detect-the-last-iteration-in-a-loop/55543/3 "2021-12-16T20:44:07Z")

</div>

Ahhh I see, thank you very much for that code snippet!  
Instead of appending the Slash at the end of the iteration, you add it at the beginning, starting with the second iteration (to avoid the Slash at the very first position).

I have never coded it that way before, but as long as it works 😇

Could you also tell me how do you solve the points a) to c) ?
