Editing multi-value tags, replace only one occurrence

Hello,

I’m currently trying to modify the genre tag in my collection. I’ve used multiple genres for most of my MP3s and in many cases the last “genre” in the tag is actually the BPM. So for example one of my files has: Acid; Trance; Jungle; 165

This worked fine in MusicBee, but now I’m moving to a different music player (fooyin for Linux) and it interprets the BPM number as the ID3v1 genre. So my previous example now looks like: Acid; Trance; Jungle; IDM

I’m working on a script to move the BPM to the actual BPM tag, and remove it from the genre tag. This seems to be working fine for the most part, however in some cases I have a genre in the genre tag that matches the ID3v1 genre, e.g. Downtempo; Ambient; Jazz; 155 which is seen as Downtempo; Ambient; Jazz; Downtempo. I want to remove the last instance of Downtempo and not both, however $replacemulti targets all occurrences. How can I remove just one?

For reference, here’s my script so far:

$if($gt($lenmulti(%genre%),1),
    $set(_last,$getmulti(%genre%,-1))
    $if($eq(%_last%,Folk),
        $set(BPM,80)
        $replacemulti(%genre%,Folk,)
    )
    $if($eq(%_last%,Bebop),
        $set(BPM,85))
        $replacemulti(%genre%,Bebop,)
    )
    $if($eq(%_last%,Avantgarde),
        $set(BPM,90)
        $replacemulti(%genre%,Avantgarde,)
    )
    $if($eq(%_last%,Slow rock),
        $set(BPM,95)
        $replacemulti(%genre%,Slow rock,)
    )
    $if($eq(%_last%,Humour),
        $set(BPM,100)
        $replacemulti(%genre%,Humour,)
    )
    $if($eq(%_last%,Sonata),
        $set(BPM,105)
        $replacemulti(%genre%,Sonata,)
    )
    $if($eq(%_last%,Satire),
        $set(BPM,110)
        $replacemulti(%genre%,Satire,)
    )
    $if($eq(%_last%,Folklore),
        $set(BPM,115)
        $replacemulti(%genre%,Folklore,)
    )
    $if($eq(%_last%,Duet),
        $set(BPM,120)
        $replacemulti(%genre%,Duet,)
    )
    $if($eq(%_last%,Dance hall),
        $set(BPM,125)
        $replacemulti(%genre%,Dance hall,)
    )
    $if($eq(%_last%,Terror),
        $set(BPM,130)
        $replacemulti(%genre%,Terror,)
    )
    $if($eq(%_last%,Beat),
        $set(BPM,135)
        $replacemulti(%genre%,Beat,)
    )
    $if($eq(%_last%,Contemporary Christian),
        $set(BPM,140)
        $replacemulti(%genre%,Contemporary Christian,)
    )
    $if($eq(%_last%,Anime),
        $set(BPM,145)
        $replacemulti(%genre%,Anime,)
    )
    $if($eq(%_last%,Baroque),
        $set(BPM,150)
        $replacemulti(%genre%,Baroque,)
    )
    $if($eq(%_last%,Downtempo),
        $set(BPM,155)
        $replacemulti(%genre%,Downtempo,)
    )
    $if($eq(%_last%,Electroclash),
        $set(BPM,160)
        $replacemulti(%genre%,Electroclash,)
    )
    $if($eq(%_last%,IDM),
        $set(BPM,165)
        $replacemulti(%genre%,IDM,)
    )
    $if($eq(%_last%,Leftfield),
        $set(BPM,170)
        $replacemulti(%genre%,Leftfield,)
    )
    $if($eq(%_last%,Post-punk),
        $set(BPM,175)
        $replacemulti(%genre%,Post-punk,)
    )
    $if($eq(%_last%,Trop rock),
        $set(BPM,180)
        $replacemulti(%genre%,Trop rock,)
    )
    $if($eq(%_last%,Neue Deutsche Welle),
        $set(BPM,185)
        $replacemulti(%genre%,Neue Deutsche Welle,)
    )
    $if($eq(%_last%,Garage rock),
        $set(BPM,190)
        $replacemulti(%genre%,Garage rock,)
    )
)

Thanks in advance for any assistance!

Update: I’ve gotten it working by joining, using regex, then setting.

$set(_all,$join(%genre%,; ))
$if($eq(%_last%,Downtempo),
    $set(bpm,155)
    $delete(genre)
    $setmulti(genre,$rreplace(%_all%,; Downtempo\$,))
)