View previous topic :: View next topic |
Author |
Message |
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Thu Apr 22, 2004 1:25 pm Post subject: 'IF' state bug? |
|
|
_______________________________________________________________________
I just realized there's a problem using "IF" alone as
some of us do (if %%string etc.) on some strings.
While this shows TRUE for visible chars, it shows FALSE
for strings containing only whitespace chars (including
blanks).
Functions such as @len() @not() @null() should be
used to check strings that may only contain whitespace
chars.
In my opinion this inconsistent behavior is a bug. If mods
don't think so, please move this post to the "tips and tricks"
section since there's a lot of posted code using this method.
EXAMPLE:
Code: |
rem -- simple CRLF/TAB/BLANK 'IF' test --
%%string = @chr(13)@chr(10)" "@cr()@tab()@chr(9)
%%msg = "%%string length = "@len(%%string)" chars"@cr()@cr()
if %%string
%%msg = %%msg"IF state = TRUE (should be TRUE)"
else
%%msg = %%msg"IF state = FALSE (should be TRUE)"
end
INFO %%msg
|
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
Skit3000 Admin Team
Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
|
Back to top |
|
|
jules Professional Member
Joined: 14 Sep 2001 Posts: 1043 Location: Cumbria, UK
|
Posted: Thu Apr 22, 2004 4:33 pm Post subject: |
|
|
Skit3000 wrote: | I think the "if" command will automaticly trim parameters down (like many other functions do), because otherwise "if @equal(1, 1)" (watch the space after the comma) would be FALSE, while it should be TRUE... |
That's exactly right. In fact I remember an early version of VDS which didn't trim spaces, which caused problems when people had a couple of blanks on the end of the line.
The solution is to put single quotes round anything if you want spaces to be included in the comparison. For example:
Code: | %A = " "
if @equal('%A','')
info empty
else
info non-empty
end |
reports "non-empty" as expected. Single quotes are treated as part of the text, unlike double-quotes which have a special significance when strings are evaluated.
On a vaguely related topic, it's worth mentioning that the @equal function will report that 0 is equal to . (in most of the world.) I've been bitten by this a few times.
The reason is that the function tries to do a numeric comparison if both things are numeric, and the numeric evaluation routine considers "." = "0.0" = "0".
To force an alphanumeric comparison, again use single quotes '%A' which will ensure that the values are not treated as numbers.
It's not a bug, it's a result of the fact that variables in VDS are untyped, so VDS has to decide for itself if a value is supposed to be numeric or not. Sometimes the logic it uses is not what you intended... _________________ The Tech Pro
www.tech-pro.net |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Fri Apr 23, 2004 1:11 pm Post subject: |
|
|
jules wrote: |
The solution is to put single quotes round anything if you want spaces to be included in the comparison. |
Well, if ya notice, there ARE quotes around the space
in %%string:
%%string = @chr(13)@chr(10)" "@cr()@tab()@chr(9)
I realize using @equal() and other string functions (as I
mentioned) works.
It's the inconsistency with IF %%VAR that I'm talking
about. Apparently it trims the var whether quoted or
not.
Cheers, Mac
[EDIT] - removed the reference to enclosing the whole
string in quotes - that obviously works because it makes the
function names a literal string. I had two examples - changed
one and tested the other... _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Last edited by Mac on Sat Apr 24, 2004 12:30 pm; edited 1 time in total |
|
Back to top |
|
|
Serge Professional Member
Joined: 04 Mar 2002 Posts: 1480 Location: Australia
|
Posted: Sat Apr 24, 2004 12:34 am Post subject: |
|
|
hi mac,
hum...have you tried single quotes...jules mentioned using single quotes while your example showed double quotes
serge _________________
|
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Sat Apr 24, 2004 12:38 pm Post subject: |
|
|
Hey serge,
Of course it shows TRUE by adding one or more single
quotes - a single quote is a visible char.
However, this also changes the actual content of the string,
and will always show TRUE - which negates the purpose of
the IF check...
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
Skit3000 Admin Team
Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
Posted: Sat Apr 24, 2004 1:20 pm Post subject: |
|
|
Mac, it doesn't always show TRUE, since it shows VDS this way that it can't trim down the contents of a variable:
Code: | %A = " "
if @equal('%A','')
info empty
else
info non-empty
end |
Above code will not change the content of the variable %A, since the single quotes are added to the @equal() function, and not to the variable... _________________ [ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial! |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Sat Apr 24, 2004 3:15 pm Post subject: |
|
|
Hey Skit,
Ya missed the point entirely.
Using functions such as @equal(), @len(), @not(), and
@null() DOES WORK with quoted blanks or non-visible
chars - as I've already mentioned a couple of times.
Using IF alone is where the inconsistency is:
Code: | if %%string
INFO String is not empty
else
INFO String is empty
end |
Has anyone even tried the example in my first post?
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
jules Professional Member
Joined: 14 Sep 2001 Posts: 1043 Location: Cumbria, UK
|
Posted: Sat Apr 24, 2004 5:28 pm Post subject: |
|
|
The reason the functions can work like that is because there are brackets to show the start and end of the string. For example:
Code: | %A =
if @null(%A)
info empty
else
info non-empty
end |
returns "empty", whereas:
Code: | %A =
if @null( %A )
info empty
else
info non-empty
end |
returns "non-empty".
I'm not sure I'm even convinced that's the right approach, since most people familiar with the way other programming languages work would probably expect both examples to give the same result.
The if command trims the string in order to remove any extra blanks which would make the result always true (non-null) whatever the expression evaluated to. Not only are trailing blanks trimmed off, but also any extra blanks between the command and the rest of the command line.
When a command (any command) is evaluated, the contents of any variables or the result of any functions have already been inserted into the command line, e.g. %A has been replaced by its actual contents. At this point, the if command cannot distinguish between blanks that were in a variable and extraneous ones on the command line, so all leading or trailing blanks are trimmed, unless you do what I suggested to ensure that the test that is used retains them.
The if command has worked this way since 1996, so you better get used to it. If it didn't, none of the example code copied from this forum would work because it appears to add a trailing blank to each line of code. But more importantly, a lot of people would waste a lot of time chasing errors where the if command wasn't doing what they expected, because of blanks on the line that they aren't aware of. Believe me, I've been there. _________________ The Tech Pro
www.tech-pro.net |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Sat Apr 24, 2004 6:27 pm Post subject: |
|
|
jules wrote: | The if command has worked this way since 1996, so you better get used to it. |
Well, nothing else trims blanks that are quoted - except the
@trim() function. So whatever the reason, it's inconsistent
behavior (better get used to the idea). It prolly should cause
a "missing function" error to avoid misconceptions, or at least
have a note in the help file.
BTW, I'm not surprised it won't be changed - I just wanted
other users to be aware of it. Like I said, if mods don't think
it's a bug, this should prolly be put in the Knowledge Base
("tips and tricks") section.
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Last edited by Mac on Sun Apr 25, 2004 2:17 pm; edited 1 time in total |
|
Back to top |
|
|
Skit3000 Admin Team
Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Sun Apr 25, 2004 2:18 pm Post subject: |
|
|
Thanks Skit.
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|