Scripting Bugs
Forum rules
READ THIS AND DO IT!!!
Please include the following information:
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
READ THIS AND DO IT!!!
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).
Re: Scripting Bugs
It appears that the second condition is unnecessarily evaluated even if not required
if (1==2 && property("#11", "<curitem>") == "Picture") {
echo "Should never execute - but I checked both" ;
} else {
echo "I took a little longer to come here" ;
}
if (1==2 && property("#11", "<curitem>") == "Picture") {
echo "Should never execute - but I checked both" ;
} else {
echo "I took a little longer to come here" ;
}
-
PeterH
- Posts: 2826
- Joined: 21 Nov 2005 20:39
- Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%
Re: Scripting Bugs
I would not see this as a (kind of) bug.
I think XY does what's written: it calculates the value of the boolean expression - and then decides the 'if'.
Though it might be a bit more performant, if the 2nd operand of an 'and' will not be calculated if the first is '0'. But bad (for you), if the operands are calculated right to left
(Don't say this is not allowed...
)
I think XY does what's written: it calculates the value of the boolean expression - and then decides the 'if'.
Though it might be a bit more performant, if the 2nd operand of an 'and' will not be calculated if the first is '0'. But bad (for you), if the operands are calculated right to left
Re: Scripting Bugs
If it's right to left, I can simply switch them. But it doesn't appear to be that way either
-
admin
- Site Admin
- Posts: 65456
- Joined: 22 May 2004 16:48
- Location: Win8.1, Win10, Win11, all @100%
- Contact:
Re: Scripting Bugs
This optimization could be too smart. Look at this (air code). You might want to try the second write even if the first failed.
Code: Select all
if (writefile($file1, $data1) == 0 && writefile($file2, $data2) == 0) {
echo "Both failed!";
}FAQ | XY News RSS | XY X
Re: Scripting Bugs
That air code would say both failed even if one succeeded. If I would want one stuff to be completed regardless of other, I wouldn't put it in relation to the first.
It would be good if that smartness can be put in place - this would avoid expensive operations to the right if the success/failure of the condition has already been decided.
Edit: We can split the operation in two (but that requires copying the else as well)
It would be good if that smartness can be put in place - this would avoid expensive operations to the right if the success/failure of the condition has already been decided.
Edit: We can split the operation in two (but that requires copying the else as well)
-
admin
- Site Admin
- Posts: 65456
- Joined: 22 May 2004 16:48
- Location: Win8.1, Win10, Win11, all @100%
- Contact:
Re: Scripting Bugs
No. Try this:
Code: Select all
$file1 = "a*.txt"; $file2 = "b.txt"; $data1 = "X";
if (writefile($file1, $data1) == 0 && writefile($file2, $data1) == 0) {
echo "Both failed!";
}FAQ | XY News RSS | XY X
-
admin
- Site Admin
- Posts: 65456
- Joined: 22 May 2004 16:48
- Location: Win8.1, Win10, Win11, all @100%
- Contact:
Re: Scripting Bugs
This is what I meant by "This optimization could be too smart.". I cannot know what the code intended or expects. And I think the default assumption is that both parts of the AND-term are processed.
You can easily optimize your code by nesting:
Instead of
you do:
You can easily optimize your code by nesting:
Instead of
Code: Select all
if A and B thenCode: Select all
if A then
if B thenFAQ | XY News RSS | XY X
Re: Scripting Bugs
Correct. After numerous edits.
1 and 1 = 1
0 and 0 = 1
I should use the Or operator.
Edit: Note to self - Read the truth tables again.
1 and 1 = 1
Edit: Note to self - Read the truth tables again.
Last edited by nerdweed on 18 May 2014 21:50, edited 1 time in total.
-
admin
- Site Admin
- Posts: 65456
- Joined: 22 May 2004 16:48
- Location: Win8.1, Win10, Win11, all @100%
- Contact:
Re: Scripting Bugs
0 and 0 = 1 ???
In which part of the universe do you live?
In which part of the universe do you live?
FAQ | XY News RSS | XY X
Re: Scripting Bugs
Oh, I was correct earlier. I thought I boo-ed earlier but boo-ed on the second instance.
But if first is false, why should the second be checked - hasn't the failure been determined.
If (1==0 && 1==1) {
;
}
When first is true, why check for the second.
if (1==1 || 1==0) {
;
}
Shouldn't this be short circuit operators like in PHP
The problem with nesting is the Else part.
Gravity pushed me at that instance.In which part of the universe do you live?
But if first is false, why should the second be checked - hasn't the failure been determined.
If (1==0 && 1==1) {
;
}
When first is true, why check for the second.
if (1==1 || 1==0) {
;
}
Shouldn't this be short circuit operators like in PHP
The problem with nesting is the Else part.
-
PeterH
- Posts: 2826
- Joined: 21 Nov 2005 20:39
- Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%
Re: Scripting Bugs
Basically this is a bool expression - and is simply calculated by bool logic. I.e. it is and AND instruction - and is executed even if the first operand of it is false.nerdweed wrote:But if first is false, why should the second be checked - hasn't the failure been determined.
For your wish the evaluation logic would have to be expanded to test the 1st operand for false, and in this case abend the bool calculation. This would be analog to the "nested if" Don gave as an example. And would have the implications Don mentioned.
Re: Scripting Bugs
I have a real life example where I could have used what nerdweed suggests. In a script I get the recent paths and I would like to create a list about the existing folders. If the actually checked folder is a network folder, I want to add it to my list, without further check, assuming that it is existing. (because the exists(network_folder) slows the script down significantly) I tried this:
If the first part is true, no need the run the second part, the whole expression will be true, because of the "or". But XY runs the second part anyway, slowing down the script significantly. I had to rewrite the script in an unfortunate way:
As you can see I had to double the adding part.
The "add the path to my list" part is not as simple as I show it in my example, so I have to keep the two part in sync manually.
Code: Select all
if (substr($NextPath,0,2) == "\\" or exists($NextPath) > 0) {add the path to my list}Code: Select all
if (substr($NextPath,0,2) == "\\") {add the path to my list}
elseif (exists($NextPath) > 0) {add the path to my list}
else {not accessible path, go to the next one}-
admin
- Site Admin
- Posts: 65456
- Joined: 22 May 2004 16:48
- Location: Win8.1, Win10, Win11, all @100%
- Contact:
Re: Scripting Bugs
No, what you did is good coding. To avoid the doubling of code simply use a little local flag:
Code: Select all
if (substr($NextPath,0,2) == "\\") {$add = 1;}
elseif (exists($NextPath) > 0) {$add = 1;}
else {$add = 0;}
if ($add == 1) {add the path to my list}FAQ | XY News RSS | XY X
Re: Scripting Bugs
Why is it so obvious after you have written it down? And why not before?admin wrote:To avoid the doubling of code simply use a little local flag
XYplorer Beta Club