Condition in if loop not evaluated correctly

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: 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%).

:info: Use Your Profile Or Signature
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.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Condition in if loop not evaluated correctly

Post by Marco »

Code: Select all

 $orig = readfile ("C:\Dati\Marco\Programmi\Firefox\Data\profile\adblockplus\patterns.ini");
 $first = gettoken ($orig,1,<crlf>);
 $second = gettoken ($orig,2,<crlf>);
 if ($first.<crlf>.$second != "# Adblock Plus preferences".<crlf>."version=4") {
  echo nope!;
 };
I'm trying to parse the filters file of Adblock Plus. Its header is the following

Code: Select all

# Adblock Plus preferences
version=4
so I want to check this before proceeding with further elaborations. But, stepping, I see that the SC engine resolves to this

Code: Select all

if
----
# Adblock Plus preferences
1
version=4
Looks like the truth value is interpolated into the right side of the condition.
Am I doing something wrong or there's a bug?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Condition in if loop not evaluated correctly

Post by Stefan »

Perhaps you have tricked out the engine. Try proper quoting:

"$first<crlf>$second" != "# Adblock Plus preferences<crlf>version=4"

Code: Select all

 $orig = "# Adblock Plus preferences".<crlf>."version=4";     step;
 //readfile ("C:\Dati\Marco\Programmi\Firefox\Data\profile\adblockplus\patterns.ini");
 
 $first  = gettoken ($orig,1,"<crlf>"); 
 $second = gettoken ($orig,2,"<crlf>");

 if ("$first<crlf>$second" != "# Adblock Plus preferences<crlf>version=4") {echo "nope!";}else{msg "yes";}

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Condition in if loop not evaluated correctly

Post by Marco »

:oops:
Thanks! Looks like that was the only combination of quotes I hadn't tried before.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

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

Re: Condition in if loop not evaluated correctly

Post by PeterH »

It seems Stefans solution is right - but the reason isn't :shock:

Just to understand what happens the right reason: "Operator Precedence"!
The String Concatenator "." has the lowest precedence of all operators. (Especially it's lower than "!=".)

So:
if ($first.<crlf>.$second != "# Adblock Plus preferences".<crlf>."version=4")
is the same as writing:
if ($first.<crlf>. ($second != "# Adblock Plus preferences") .<crlf>."version=4")
And no doubt: this cannot lead to the correct result.
To make it formally correct (without further change) you would have to write:
if (($first.<crlf>.$second) != ("# Adblock Plus preferences".<crlf>."version=4"))

Of course Stefans solution of putting <crlf> inside (the) double quotes, and by this not needing concatenation characters (conflicting with !=) looks better:
if ("$first<crlf>$second" != "# Adblock Plus preferences<crlf>version=4")


By the way: I would have coded:
if ( ($first != "# Adblock Plus preferences") || ($second != "version=4") )

Post Reply