Why output is evaluated as zero unless floor() etc

Discuss and share scripts and script files...
Post Reply
Huidong
Posts: 213
Joined: 18 May 2011 21:55

Why output is evaluated as zero unless floor() etc

Post 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?

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

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

Post 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.
One of my scripts helped you out? Please donate via Paypal

Huidong
Posts: 213
Joined: 18 May 2011 21:55

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

Post 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!

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

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

Post 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'.

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

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

Post 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.

Huidong
Posts: 213
Joined: 18 May 2011 21:55

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

Post 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 :)

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

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

Post by PeterH »

If you want to see the exact contents of the variable:

Code: Select all

 echo hexdump($output);

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

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

Post 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]*'

Huidong
Posts: 213
Joined: 18 May 2011 21:55

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

Post 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...)

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

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

Post 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.

Huidong
Posts: 213
Joined: 18 May 2011 21:55

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

Post 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.

Post Reply