Ah! Another glorious chance to display my lacking understanding of scripting and it’s terminology
I have a tag containing data like:
blablabla; blablabla2, whatever: interesting
Obviously I only want ‘interesting’ returned here.
Going through the documentation, this seemed a likely candidate:
$substr(text,start,end)
“Returns the substring beginning with the character at the start index, up to (but not including) the character at the end index. Indexes are zero-based. Negative numbers will be counted back from the end of the string. If the start or end indexes are left blank, they will default to the start and end of the string respectively.”
So I created:
$set(nicetag,$substr(%sourcetag%,:,))
But that will return the complete string, not only whats after the colon.
(p.s., I don’t require the space right after the colon either)
I thought to enter something for ‘end’ too, but failed with that too.
What am I doing wrong?
There’s something implicitly documented here: the indexes are numbers (the first character in a string has index 0, the second one index 2, etc.). If you pass something as an index that can’t be converted to a number, Picard ignores that value.
Assuming that there’s only ever a single colon in the string, you want
This first finds the index of the first colon, saves it as _indexofcolon, uses that as an index and (since you probably don’t want the whitespace after the colon to be part of the new value), strips the whitespace with $strip (if it exists).
/edit: Fixed the off-by-one error mentioned by others.
$substr is expecting integers for start and end, but you are providing the character ‘:’. To do what you want (return everything after the first occurrence of a colon), you would need something like:
Thanks guys!
I am going to try all proposed solutions, and stick to the one that seems to works best for me.
(and I hope to learn from it in the process…)
update;
I just asked my brain, and it says it likes the solution with the $trim function the most.
Working great.
I was in the process of writing my response, but you beat me to it.
One thing though… I think that your code will also return the colon at the start of the string, so you would have to add the colon to the $strip characters.
Also, I’ve added the bit about the inputs being integers to the draft Picard documentation that I’ve started working on. The project is on GitHub.
It works fine for tags that indeed contain a colon:
But if the source tags has no colon, it will return the source string, but it will also chop-off the last character:
So I probably need to add some ‘if-contains-colon’ statement here.
I should be able to figure that out with a clear mind, but for now I was wondering if this behaviour of $substr is correct, in that it removes the last character when ‘:’ is not matched?
I think you’re actually missing a comma in the script line you provided because it causes an error as shown.
The problem is that $find is returning “-1” when the search string (colon) is not found, and you are entering this as the end parameter to $substr which means “one character before the end of the string”. For what you want, try something like:
That raises a good point. Perhaps $find should return “” if the string is not found, rather than “-1”. I wrote it to return “-1” because that’s the way the function works in Python.
And your code misses an % (after %_temp )
( Let’s pick up golf together? )
No, I’m sorry, kidding of course. Thank you for continued support and speedy and informative replies!
I’m going to digest this all very soon. (not tonight though)
Just one more brief and impulsive brainfart for today:
Would the addition of a new function like $split perhaps be useful?
“split before a certain character/split after a certain character”
I’m accustomed to it from another piece of software, and I find it useful and not too hard to understand.
Nah, I’ll leave it up to you guys to estimate if you think something like that might be beneficial to more users.
I can also figure out using regex once in a while for some cases, but some days regex comes easy to me, and some days it makes me pulling my hairs out