0

Lets say I have a string like this:

And I need to add and delete names at will. What would be the better way of doing it without the risk of leaving any leading ";" here and there? Since, depending on its position, the name could be preceded for a ";" or not... Well, my first tries are being something in this line:

<script src="//github.com/fengari-lua/fengari-web/releases/download/v0.1.4/fengari-web.js"></script>
<script type="application/lua">

    local names = " Pic One; Pic Three; Pic Eight; Pic Two" -- The first emoji results missing after processing
    local picName = " Pic One"

    if not names:match(picName) then
        names = names .. ";" .. picName --addition
    else
        names = names:gsub(";?" .. picName, "") --deletion
    end
    names  = names:sub(1, 1) == ";" and names:sub(2) or names -- Instead of my first try: names = names:sub(2)
    print(names)

</script>

But I'm sure there is an approach out there much more reliable and elegant (pattern based, for example) I'm not finding, so here I'm...

EDIT¹: New code accordingly to @shingo suggestion (thanks!), which seems to work fine, EXCEPT when the first name starts with emoji, cause it results removed for some reason...

EDIT²: A try to fix the first missing emoji issue that seems to wor- erm... not, it seems it gets added again instead of being removed as can be seen... I think now the problem is I'm removing the fist ";" and, as the code is right now, it expects to find everything starting with a ";"? But I'd like not to store that first leading ";" if possible for the exposed reasons.

Rai
  • 314
  • 1
  • 2
  • 9
  • Why are you confident? Honestly you'd better process a table rather than a string in lua. – shingo Feb 27 '23 at 04:44
  • A simple approach is always adding or deleting ";Name", then trim the leading ";" for the final output. – shingo Feb 27 '23 at 04:45
  • @shingo Oh, sorry, I may should have clarify... The reason for using a string instead a table is that's the way the main app seems to be storing this "multi" kind of settings and I wanted to follow the style as long as possible, thanks! – Rai Feb 27 '23 at 04:51
  • @shingo Thank you! I've edited my question accordingly to that and it seems I'm almost there... except for the emoji issue I comment, maybe I'm being too generic by removing any first character and I should be more specific or something? – Rai Feb 27 '23 at 05:27
  • You can do some postprocess on the string by prepending a ';' if the input string is not empty. – shingo Feb 27 '23 at 05:45
  • @shingo Hmm... I think that may work, yes. But do you think there could be any other easy way for not to have to store that first ";"? Because that's the way the main app is doing it after all... – Rai Feb 27 '23 at 05:53
  • 1
    This is easy enough by my standards, for deletion you can use `names:gsub(";?" .. picName, "")`. – shingo Feb 27 '23 at 11:45
  • @shingo Yeah, it seems to work! I still have to test if everything works well under any possible situation, e . g. should some wild leading ";" remain here or there for whatever reason (although I don't think it should, isn't it?), but I think this is a good enough starting point at least, so thanks again! – Rai Feb 27 '23 at 14:03

0 Answers0