Weird behavior found with $if() and $strstr()

I am creating an action group for formatting my tags and has just started playing with script functions.
(Using Action "Format Value", with Field TITLE just to see the output first.

My filename:
M1 - Something

Format string:

$if(0,yes,no)

Output:

no

I did this just to check if the if function accepts integer. (Edit: It doesn't. Even the value 1 will output 'no'.) Now to search for a string in the filename.

Format string:

$strstr(%_filename%,A)

Output:

0

Search for string worked. Now, to nest them.

Format string:

$if($strstr(%_filename%,A),yes,no)

Output:

yes

Now, that's weird. It should have just substituted the value of 0, but to the if function, it appears as true?

I thought that the $strstr() might be outputting a char instead an integer (if that distinction was ever in the code, I'm not sure). But I thought to try to compare the returned value if it is greater than 0.

Workaround:

$if($grtr($strstr(%_filename%,A),0),yes,no)

Output:

no

Also tried replacing the 'A' with an 'M', the output was yes, implying that the workaround succeeded.

Since it worked with the $grtr() function, shouldn't it work with the $if() function as well? I feel like this is unintentional. Fixing the $if() to work with $strstr() will surely help a lot in reducing the confusion and the script volume.

Thanks in advance!

If you use the $if() function, it is expected that the first parameter also return a boolean data type.
The example

only appears to be the correct outcome as the 0 is sometimes also taken as boolean value.
Yet, you will also get the "not true" result, even if you enter other values like e.g. "1" or "-1".
the $strstr() function returns a numeric value - which has to be compared to something when used in a boolean function.
So you would have to use $if() with one or more of the other boolean functions as referenced in the help:
https://help.mp3tag.de/main_scripting.html#boolean
So I would say: everything is fine.

2 Likes

Is it not possible to accept integer values as booleans (i.e. implicitly converting them to booleans)? I believe it would be useful to make it a feature. Or is it something that everyone has already thought of?

You already have shortcut functions like $ifgreater(), $iflonger() etc. These convert numeric results into boolean.

1 Like

I see. Thanks for the swift responses.
I have also confirmed that even the value 1 is not accepted by the if function as true. Updated the post.

Applying the $ifgreater() as @ohrenkino suggested:

$ifgreater($strstr(%_filename%,A),0,yes,no)

Huge thanks!