[resolved] sprintf doesn't work with variable number of arguments

Options
My assumption was that if you have more arguments than you have strings-to-substitute in a sprintf, it will just ignore the extra arguments.

e.g. suppose my string is
I have two items, %s and %sbut I pass this to sprintf with three parameters "one", "two", and "three".

I assumed it would just ignore the "three", but instead it throws an error:
[Screen Shot 2021-12-27 at 10.10.00 PM.png]
I would also hope it doesn't fail in the other direction, e.g. suppose my string is
I have three items: %s, %s, and %sand I only pass this to sprintf with two parameters "one" and "two", I would hope it leaves the last %s in the original string intact.

I currently have a bunch of strings with a variable number of %s's and I know beforehand what each of the parameters will be even if the string doesn't require all of them. It would be nice if this doesn't error out and instead ignores extra/missing parameters.

Comments

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     how are you going about trying to make variable number of arguments? Can you please share some screenshots or a video because I'm not even sure how this would even be set up.

    sprintf requires you to add the arguments within the filter. I'm not seeing a way to attempt to make that a variable number. If you have three %s then you would have three arguments on 1 sprintf filter... 

    perhaps consider adding conditional logic to your function stack to accomplish this
  •  Sorry, my wording was confusing.

    I am always passing three parameters, but the particular string being sprintf-ed may not have three %s elements to replace (it has a maximum of 3).
  •  [Screen Shot 2021-12-28 at 4.59.31 PM.png]
    The string being sprintf-ed, "tag_history", could have anywhere between 0 and 3 %s elements to replace. 
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     will the string format be standard for each instance of 1, 2, or 3 %s's? Or will they vary?
  •  No, they're different sentences, but the order of the elements to be replaced is always the same.
  •  As a workaround I guess I can just check for the number of instances of %s and break it into 3 cases before doing the sprintf.
  •  I ended up using the replace function instead of the sprintf function (and changing each %s into something more specific to be replaced), which works even if the thing you're replacing isn't in the original string.
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     great - sounds like a clever solution!
  • Sean Montgomery
    Sean Montgomery Administrator

    ADMIN

    Options
     seems like we just need the protection when there arent enough variables. We can add support so that extra arguments don't cause an issue. Good idea.
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     - we have added support so that the extra arguments don't throw an error