Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Title formatting - boolean values and $or() $if() (Read 658 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Title formatting - boolean values and $or() $if()

In the reference I can read the following example:

Code: [Select]
#False: 
$if(0,True,False)
# False:
$if('0',True,False)
# True or False:
[$add(%rating%,1)]

I gather from this example that the value 0 is considered 'falsy'.

I try the following code, the purpose of which is to establish whether the source of the sound is a radio stream or a local file:

Code: [Select]
$puts(isHttp,$ifequal($strstr($lower(%_path_raw%),'http://'),1,1,0))
$puts(isHttps,$ifequal($strstr($lower(%_path_raw%),'https://'),1,1,0))
$puts(isRadioStream,$if($or($get(isHttp),$get(isHttps)),1,0))

isHttp = $get(isHttp)$crlf()
isHttps = $get(isHttps)$crlf()
isRadioStream = $get(isRadioStream)$crlf()

$crlf()$crlf()

$if($get(isRadioStream),RADIO STREAM,LOCAL FILE)

X

As you can see in the screenshot, the value of IsRadioStream is 1, but this is incorrect; it should be 0.

To get around the error I modify the code like this:

Code: [Select]
$puts(isHttp,$ifequal($strstr($lower(%_path_raw%),'http://'),1,1,0))
$puts(isHttps,$ifequal($strstr($lower(%_path_raw%),'https://'),1,1,0))
$puts(isRadioStream,$if($or($greater($get(isHttp),0),$greater($get(isHttps),0)),1,0))

isHttp = $get(isHttp)$crlf()
isHttps = $get(isHttps)$crlf()
isRadioStream = $get(isRadioStream)$crlf()

$crlf()$crlf()

$if($get(isRadioStream),RADIO STREAM,LOCAL FILE)

X

Now the value of isRadioStream is correct (0, "falsy", as we saw earlier), but the $if() instruction returns the wrong result (RADIO STREAM).

Am I doing something wrong or is there a problem with the title formatting engine?

Re: Title formatting - boolean values and $or()

Reply #1
In the reference I can read the following example:

Code: [Select]
#False: 
$if(0,True,False)
# False:
$if('0',True,False)
# True or False:
[$add(%rating%,1)]

I gather from this example that the value 0 is considered 'falsy'.

I try the following code, the purpose of which is to establish whether the source of the sound is a radio stream or a local file:

Code: [Select]
$puts(isHttp,$ifequal($strstr($lower(%_path_raw%),'http://'),1,1,0))
$puts(isHttps,$ifequal($strstr($lower(%_path_raw%),'https://'),1,1,0))
$puts(isRadioStream,$if($or($get(isHttp),$get(isHttps)),1,0))

isHttp = $get(isHttp)$crlf()
isHttps = $get(isHttps)$crlf()
isRadioStream = $get(isRadioStream)$crlf()

$crlf()$crlf()

$if($get(isRadioStream),RADIO STREAM,LOCAL FILE)

[attach type=image]35196[/attach]

As you can see in the screenshot, the value of IsRadioStream is 1, but this is incorrect; it should be 0.

To get around the error I modify the code like this:

Code: [Select]
$puts(isHttp,$ifequal($strstr($lower(%_path_raw%),'http://'),1,1,0))
$puts(isHttps,$ifequal($strstr($lower(%_path_raw%),'https://'),1,1,0))
$puts(isRadioStream,$if($or($greater($get(isHttp),0),$greater($get(isHttps),0)),1,0))

isHttp = $get(isHttp)$crlf()
isHttps = $get(isHttps)$crlf()
isRadioStream = $get(isRadioStream)$crlf()

$crlf()$crlf()

$if($get(isRadioStream),RADIO STREAM,LOCAL FILE)

[attach type=image]35194[/attach]

Now the value of isRadioStream is correct (0, "falsy", as we saw earlier), but the $if() instruction returns the wrong result (RADIO STREAM).

Am I doing something wrong or is there a problem with the title formatting engine?
The value 0 or 1 in isHttp is not FALSE or TRUE. It has a value of 0 or 1. It can be used in the way you want but not with $if.
Use $ifequal($get(isHttp),1,STREAMPART,NORMALPART) instead.

or alternatively use:

$if($strstr(%path%),http),STREAMPART, NORMALPART). In this case the $strstr will return empty/false  (and not the values 0/1) when %path% is not a stream.

If you want to be able to diversify  streams also for youtube you will need more code btw.

Re: Title formatting - boolean values and $or() $if()

Reply #2
Unfortunately the wiki page is third party written and it's misleading and confusing. There are no boolean values. $if() triggers the first option if it gets any existing titleformat data, be it a value from a tag or any output from a function, the contents are irrelevant.

Re: Title formatting - boolean values and $or() $if()

Reply #3
Would it be valid to consider "$if()" as (more meaningfully) "$ifexists()"?
It's your privilege to disagree, but that doesn't make you right and me wrong.

Re: Title formatting - boolean values and $or() $if()

Reply #4
Would it be valid to consider "$if()" as (more meaningfully) "$ifexists()"?
Hmmm don't know about that.

For instance $if(%bla% will evaluate to true even when %bla% does not exist,  because %bla% will return ? which is data so is TRUE.
To get the desired behavior you should use $if([%bla%] in this case. This will return NULL when %bla% does not exist.

Re: Title formatting - boolean values and $or() $if()

Reply #5
Would it be valid to consider "$if()" as (more meaningfully) "$ifexists()"?
Hmmm don't know about that.

For instance $if(%bla% will evaluate to true even when %bla% does not exist,  because %bla% will return ? which is data so is TRUE.
To get the desired behavior you should use $if([%bla%] in this case. This will return NULL when %bla% does not exist.
:) (this is a case where a "like"/"thanks" button would have been convenient!)
It's your privilege to disagree, but that doesn't make you right and me wrong.

Re: Title formatting - boolean values and $or() $if()

Reply #6
I'll add something to close the topic:

I repeat that the example in the reference is the following:

Code: [Select]
#False: 
$if(0,True,False)
# False:
$if('0',True,False)
# True or False:
[$add(%rating%,1)]

this is a test code that I tried on a track in my library:

Code: [Select]
%artist% - %title%
$crlf()$crlf()

// test about numbers
test 0 = $if(0,TRUE,FALSE)$crlf()
test 1 = $if(1,TRUE,FALSE)$crlf()$crlf()

// %blah% does not exist
blah = $if(%blah%,TRUE,FALSE)$crlf()

//%composer% exists but it is empty
composer = $if(%composer%,TRUE,FALSE)$crlf()

//%performer% exists and it is 0
performer = $if(%performer%,TRUE,FALSE)$crlf()

//%rating% exists and it's value is 3
rating = $if(%rating%,TRUE,FALSE)

In the screenshot you can see the test results:

X

It seems to me that it would be useful to write a few more explanatory lines in the reference.

Re: Title formatting - boolean values and $or() $if()

Reply #7
I'll add something to close the topic:

I repeat that the example in the reference is the following:

Code: [Select]
#False: 
$if(0,True,False)
# False:
$if('0',True,False)
# True or False:
[$add(%rating%,1)]

this is a test code that I tried on a track in my library:

Code: [Select]
%artist% - %title%
$crlf()$crlf()

// test about numbers
test 0 = $if(0,TRUE,FALSE)$crlf()
test 1 = $if(1,TRUE,FALSE)$crlf()$crlf()

// %blah% does not exist
blah = $if(%blah%,TRUE,FALSE)$crlf()

//%composer% exists but it is empty
composer = $if(%composer%,TRUE,FALSE)$crlf()

//%performer% exists and it is 0
performer = $if(%performer%,TRUE,FALSE)$crlf()

//%rating% exists and it's value is 3
rating = $if(%rating%,TRUE,FALSE)

In the screenshot you can see the test results:

[attach type=image]35199[/attach]

It seems to me that it would be useful to write a few more explanatory lines in the reference.
I don't know where there is a question in this, but try this and maybe that gives you an idea (USE BRACKETS) ...
[Performer %performer%$crlf()]
[Composer %composer%$crlf()]

Notice there is even  not an $if in this.

Re: Title formatting - boolean values and $or() $if()

Reply #8
I don't know where there is a question in this ...

why $if(0 -> false ?
why $if(1 -> false ?
why $if(%blah% -> false ? You said it should return '?' -> true
why $if(%performer% -> true ? It should return false so as in $if(0 (performer is an existent field set to zero)
why $if(%composer% -> false ? it should return true, as composer is an existent field

There are probably very good reasons to get those results. I'm just saying that by reading the reference it's not possible to understand what those reasons are.

Re: Title formatting - boolean values and $or() $if()

Reply #9
There are probably very good reasons to get those results. I'm just saying that by reading the reference it's not possible to understand what those reasons are.
Yes you are right. The reference is no good. That's  why I helped you out with the correct code.

Don't overthink it and just use what I typed with BRACKETS, and yes you can even skip the $if in that case.

Use this:
[Performer %performer%$crlf()]

Re: Title formatting - boolean values and $or() $if()

Reply #10
For instance $if(%bla% will evaluate to true even when %bla% does not exist,  because %bla% will return ? which is data so is TRUE.
Not correct. Missing tag field will return question mark when such tag is requested alone, but that doesn't change functionality of $if() function from what I said.
The square brackets can be thought of a shortcut for longer $if() functionality: instead of writing "$if(%artist%,artist is: %artist%,)" you can simply write "[artist is: %artist%]". Everything inside square brackets is left out if the tag referenced inside doesn't exist.

why $if(0 -> false ?
why $if(1 -> false ?
why $if(%blah% -> false ? You said it should return '?' -> true
why $if(%performer% -> true ? It should return false so as in $if(0 (performer is an existent field set to zero)
why $if(%composer% -> false ? it should return true, as composer is an existent field
For first two they are false because there are no tag fields queried. Third is incorrect, missing tag field will not trigger $if() function to treat the result as "true".
If existing but empty tag is treated like a missing field it sounds like the format where this tag is is not read. How was this test performed? foobar2000 for example will not write such tags.

 

Re: Title formatting - boolean values and $or() $if()

Reply #11
There are probably very good reasons to get those results. I'm just saying that by reading the reference it's not possible to understand what those reasons are.
Read the Titleformat document bundled by the player. It uses exact wording of how the functions work, not the interpretations written to the wiki.