Skip to main content

Topic: $if doesn't recognise string as empty (Read 1032 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
$if doesn't recognise string as empty
Strange formatting behaviour... Current track has title "01 - Party".
Code: [Select]
$left(%title%,2)

returns 01 (as expected).
Code: [Select]
$if($replace(01,01,),a,b)

returns b (as expected).
Code: [Select]
$if($replace($left(%title%,2),01,),a,b)

returns a (instead of b).
Bug or my misunderstanding?
Magically yours
Raistlin

  • Frank Bicking
  • [*][*][*][*][*]
  • Global Moderator
$if doesn't recognise string as empty
Reply #1
$if does not check for a non-empty string, but for the expression's boolean value.

%title% is true if present, which remains unchanged by string manipulation functions like $left or $replace. Boolean values are most relevant for brackets (e.g., [%tracknumber%. ]), where plain text is ignored as false if no field is present. They are also the reason why functions like $and or $longer can be used with $if without returning any text.

For use with brackets, it might be worth considering if $replace should return false for empty results, though.

Exercise: Explain why $if($replace(01,02,),a,b) returns b.

$if doesn't recognise string as empty
Reply #2
Thanks, I got it. This way it works:
Code: [Select]
$iflonger($replace($left(%title%,2),01,),0,a,b)


The main goal of mine is to check if given string consists of numbers or not. Is there a way to check this but using $replace 10 times in a row and then using $iflonger?
Magically yours
Raistlin

  • foosion
  • [*][*][*][*][*]
  • Moderator
$if doesn't recognise string as empty
Reply #3
The following code exploits the way text is parsed as a number:
Code: [Select]
$ifgreater(%title%0,%title%,a,b)

If %title% contains only digits, %title%0 will represent a number that is ten times larger than the one represented by %title%. If %title% contains only zeros, this code will incorrectly return b. This can be fixed by adding a prefix to guarantee the number is not zero:
Code: [Select]
$ifgreater(1%title%0,1%title%,a,b)

In both cases the parsing stops at the first non-digit character, i.e. "1a2" evaluates to 1 and "1a20" also evaluates to 1. Note that both code fragments return b for negative numbers and they disagree on the empty string.
http://foosion.foobar2000.org/ - my components for foobar2000

$if doesn't recognise string as empty
Reply #4
foosion
Quote
they disagree on the empty string

Empty string returns FALSE which is correct - it doesn't consist of numbers. So I don't see what to note here.
Thank you very much for your explanations!
Magically yours
Raistlin