# Counting tracks

**URL:** https://community.mp3tag.de/t/counting-tracks/66246
**Category:** Web Sources Discussion
**Created:** [October 12, 2024, 10:00pm UTC](https://community.mp3tag.de/t/counting-tracks/66246 "2024-10-12T22:00:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![yorickausyps](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/y/f6c823/32.png) [@yorickausyps](https://community.mp3tag.de/u/yorickausyps)
#### Post date: [October 12, 2024, 10:00pm UTC](https://community.mp3tag.de/t/counting-tracks/66246/1 "2024-10-12T22:00:04Z")

</div>

The following is an example for what can be done with the new feature to reference the content of output buffer via `%output%`, but to use the full potential of this feature it needs another hack to **load the content of output buffers into the input buffer** and process it with other web source script commands.  
The discogs web source does not deliver the total number of tracks of a release. To show how to number the tracks of a release with 5 tracks like 1/5, 1/5, etc. in a script using the discogs WS, we assume we have the following output buffers:

```auto
set "tracks" "|||||"
set "TRACK" "1|2|3|4|5|"

```

Our job now is to count the number of vertical bars in the buffer "tracks" and then to append that number with a slash to the numbers in buffer "TRACK".  
The only way (I know so far) to count something in the web source script language is the command `json_select_many_count`, and that needs a json array as input. Therefore we convert "tracks" into a json array. For this we

1. "load" its content `%tracks%` into the input buffer,
2. enclose every vertical bar with quotation marks and separate them with commas
3. surround all with a json array structure
4. remove the last superflous comma

```auto
	regexpreplace "^.*" "%tracks%"
	replace "|" "\"|\","
	regexpreplace "^.*" "{\"array\":[${0}]}"
	replace ",]" "]"

```

Now the input buffer contains the desired json array

```auto
{"array":["|","|","|","|","|"]}

```

and we can go on with

```auto
	json "on" "current"
	json_select_many_count "array"
	set "Totaltracks"
	outputto "Totaltracks"
	sayrest

```

To append `/%Totaltracks%` to the track positions we again "load" this time the content of `%TRACK%` into the input buffer and insert the /5 before the vertical bars.

```auto
	regexpreplace "^.*" "%TRACK%"
	replace "|" "/%Totaltracks%|"
	set "TRACK"
	outputto "TRACK"
	sayrest

```

Finally we have in TRACK:

```auto
1/5|2/5|3/5|4/5|5/5|

```

Most of these tricks and hacks I got from postings of @rboss and others, and I want to thank them all for sharing their code. Further want to suggest @Florian to make the hack to load the input buffer with regexpreplace, that I needed two times, to an official and documented command, e.g.  
`setinput String`  
because it then were easier to find and would run faster.

---

<div class="post-metadata">

### Author: ![rboss](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rboss](https://community.mp3tag.de/u/rboss)
#### Post date: [October 12, 2024, 10:45pm UTC](https://community.mp3tag.de/t/counting-tracks/66246/2 "2024-10-12T22:45:58Z")

</div>

Good idea 👍

As for your suggestion of

> [@yorickausyps](#):
>
> make the hack to load the input buffer with regexpreplace, that I needed two times

here's a small improvement: instead of having one step of loading `%tracks%` into the buffer, and another step of surrounding the buffer content with the JSON array structure, you can do both in the same `regexpreplace` command, like

```
regexpreplace "^.*" "{\"array\":[%tracks%]}"
replace "|" "\"|\","
replace ",]" "]"

```

As with your 4-step method, this will also output

```auto
{"array":["|","|","|","|","|"]}

```

in a more efficient manner.

Hope this helps.
