Page 1 of 1

Why output is evaluated as zero unless floor() etc

Posted: 03 Jun 2014 03:17
by Huidong
This question originates from a previous topic http://www.xyplorer.com/xyfc/viewtopic. ... 80#p105680

Based on the script by
klownboy
, I was playing with other MediaInfo parameters and noticed something strange:

Code: Select all

// Let MediaInfo tell the bit rate of the video, send the output to clipboard
run "cmd /c D:\MediaInfo\MediaInfo.exe --Output=Video;%BitRate% ""<cc_item>"" | clip", , 2, 0;
$output = substr("<clipboard>", 0, -1); // get rid of the line ending

echo $output; // I get the correct value (in bps, but the unit is not part of the output)
echo $output / 1024; // I get 0
echo $output + 333; // I get 333
// So for some reason, whenever doing any sort of math with $output, it is evaluated as 0, but echo $output alone gives the right result

echo floor($output) / 1024; // This fixed the problem, round() worked as well, probably ceil() too...

return round(round($output) / 1024); // OK
So this is my confusion. Why
echo $output;
works fine, but not
echo $output + 333;
? What is in $output exactly? It doesn't look like having any decimal places based on the echo result, yet the fact that it requires round() or floor() in order to behave properly in math also indicates that it isn't an integer either. So what exactly is this thing?

Re: Why output is evaluated as zero unless floor() etc

Posted: 03 Jun 2014 04:30
by highend

Code: Select all

echo $output;
Change that to

Code: Select all

echo "-" . $output . "-";
And you'll probably notice that it's not like:
-50000-
but more like e.g.:
-50000
-
And in that case it isn't internally autoconverted into an int value.

Re: Why output is evaluated as zero unless floor() etc

Posted: 03 Jun 2014 04:59
by Huidong
Damn, so there's still the line ending not stripped! Thanks for the debugging, I appreciate your help!

So instead of

Code: Select all

$output = substr("<clipboard>", 0, -1);
I should have done

Code: Select all

$output = substr("<clipboard>", 0, -2);
instead!

Re: Why output is evaluated as zero unless floor() etc

Posted: 03 Jun 2014 11:40
by PeterH
highend wrote:

Code: Select all

 echo $output;
Change that to

Code: Select all

 echo "-" . $output . "-";
Just a hint: in such cases I generally use

Code: Select all

 echo "'$output'";
 echo "output = '$output'";
(If bad to read: single quotes inside double quotes.) (Preview shows: easy to read :D )
This way you can simply 'mark' exact variable contents without breaking 'coding-flow'.

Re: Why output is evaluated as zero unless floor() etc

Posted: 03 Jun 2014 14:47
by klownboy
In my case, it's doesn't seem to matter whether I strip 1 or 2 characters from the end of clipboard contents, the output is the same. The linefeed or carriage return that we are stripping, I suppose is two characters, like ASCII DEC 13, but removing one character was enough to keep the paragraph symbol from displaying in the MediaInfo output in Custom Columns.

Re: Why output is evaluated as zero unless floor() etc

Posted: 03 Jun 2014 14:56
by Huidong
klownboy wrote:In my case, it's doesn't seem to matter whether I strip 1 or 2 characters from the end of clipboard contents, the output is the same. The linefeed or carriage return that we are stripping, I suppose is two characters, like ASCII DEC 13, but removing one character was enough to keep the paragraph symbol from displaying in the MediaInfo output in Custom Columns.
Indeed for just displaying the content, leaving the CR in the variable is not a big trouble, that's why I said echo $output was OK. But as long as you want to do any sort of math with it like unit conversion, carriage return does cause trouble (making the variable evaluated as zero), unless you use round() or floor() or ceil() on it. But instead of using round(), etc, why not just stripping the whole line ending altogether? It's neater, and doesn't cost you anything :)

Re: Why output is evaluated as zero unless floor() etc

Posted: 04 Jun 2014 00:53
by PeterH
If you want to see the exact contents of the variable:

Code: Select all

 echo hexdump($output);

Re: Why output is evaluated as zero unless floor() etc

Posted: 04 Jun 2014 15:26
by TheQwerty
Keep in mind you can use RegexReplace to extract the integer:

Code: Select all

 $string = "a<crlf>Result: 12 times<crlf>b";
 $number = RegexReplace($string, '^[\s\S]*?([-+]?\d+)[\s\S]*', '$1');
 echo $number + 10;
Or for a float/decimal use a query pattern of:

Code: Select all

^[\s\S]*?([-+]?[0-9]*\.?[0-9]+)[\s\S]*'

Re: Why output is evaluated as zero unless floor() etc

Posted: 05 Jun 2014 01:29
by Huidong
TheQwerty wrote:Keep in mind you can use RegexReplace to extract the integer:

Code: Select all

 $string = "a<crlf>Result: 12 times<crlf>b";
 $number = RegexReplace($string, '^[\s\S]*?([-+]?\d+)[\s\S]*', '$1');
 echo $number + 10;
Or for a float/decimal use a query pattern of:

Code: Select all

^[\s\S]*?([-+]?[0-9]*\.?[0-9]+)[\s\S]*
Thanks! Based on that, I played around a bit with pdfinfo, extracted the page count row out of the whole output, using the pattern

Code: Select all

^Pages:\s*\d+$
But I used regexmatches() instead, and then simply used gettoken() to get the page count as a number, loaded in a Scripted Column (trigger by Click only, just noticed that the performance of trigger by List is terrible with pdfinfo, even on an SSD hehe...)

Re: Why output is evaluated as zero unless floor() etc

Posted: 05 Jun 2014 03:22
by TheQwerty
Huidong wrote:But I used regexmatches() instead, and then simply used gettoken() to get the page count as a number, loaded in a Scripted Column (trigger by Click only, just noticed that the performance of trigger by List is terrible with pdfinfo, even on an SSD hehe...)
Based on my findings in this thread I've taken to only using RegexMatches when I have no other options - thus far that means never. ;)

Though I imagine the runret is the biggest time sink in this case, so you might want to consider using a custom tag to store or cache the desired information to limit how frequently you need to call the external tools.

Re: Why output is evaluated as zero unless floor() etc

Posted: 05 Jun 2014 23:28
by Huidong
TheQwerty wrote:
Huidong wrote:But I used regexmatches() instead, and then simply used gettoken() to get the page count as a number, loaded in a Scripted Column (trigger by Click only, just noticed that the performance of trigger by List is terrible with pdfinfo, even on an SSD hehe...)
Based on my findings in this thread I've taken to only using RegexMatches when I have no other options - thus far that means never. ;)

Though I imagine the runret is the biggest time sink in this case, so you might want to consider using a custom tag to store or cache the desired information to limit how frequently you need to call the external tools.
Thanks, good to know! So I used substr(strpos) instead. But as you guessed, the bottleneck is probably running pdfinfo itself. Using regexmatches and substr(strpos) both had list rendering issues when the script is triggered by "List". Here's a screenshot:

Image

All the PDFs are supposed to have their page count shown in the CC, which worked within the initial viewport, but once I scroll down, rendering issues start to show: and not only CC, but also Name and Ext columns are messed up as well (kind of shifted and some even superimposed hehe).

I think this can be considered a rendering bug while using the "List" trigger for CC?

I mean "Browse" is the slowest of course, but at least it doesn't have this rendering issue.