IPB

Welcome Guest ( Log In | Register )

> foobar2000 General Forum Rules

This is NOT a tech support forum.
Tech support questions go to foobar2000 Tech Support forum instead.

See also: Hydrogenaudio Terms of Service.

 
Reply to this topicStart new topic
$if doesn't recognise string as empty, and how to check if a string consists of numbers
GCRaistlin
post Jun 19 2012, 23:47
Post #1





Group: Members
Posts: 69
Joined: 24-March 12
Member No.: 98049



Strange formatting behaviour... Current track has title "01 - Party".
CODE
$left(%title%,2)

returns 01 (as expected).
CODE
$if($replace(01,01,),a,b)

returns b (as expected).
CODE
$if($replace($left(%title%,2),01,),a,b)

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


--------------------
Magically yours
Raistlin
Go to the top of the page
+Quote Post
Frank Bicking
post Jun 20 2012, 02:36
Post #2





Group: Super Moderator
Posts: 1833
Joined: 24-July 02
From: Berlin, Germany
Member No.: 2776



$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.
Go to the top of the page
+Quote Post
GCRaistlin
post Jun 20 2012, 08:50
Post #3





Group: Members
Posts: 69
Joined: 24-March 12
Member No.: 98049



Thanks, I got it. This way it works:
CODE
$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
Go to the top of the page
+Quote Post
foosion
post Jun 20 2012, 09:49
Post #4





Group: FB2K Moderator (Donating)
Posts: 4474
Joined: 24-February 03
Member No.: 5153



The following code exploits the way text is parsed as a number:
CODE
$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
$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
Go to the top of the page
+Quote Post
GCRaistlin
post Jun 21 2012, 23:01
Post #5





Group: Members
Posts: 69
Joined: 24-March 12
Member No.: 98049



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
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 22nd November 2014 - 14:24