forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


'IF' state bug?

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Bug Reports
View previous topic :: View next topic  
Author Message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Apr 22, 2004 1:25 pm    Post subject: 'IF' state bug? Reply with quote

_______________________________________________________________________
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 Smile

_________________
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
View user's profile Send private message Send e-mail
Skit3000
Admin Team


Joined: 11 May 2002
Posts: 2166
Location: The Netherlands

PostPosted: Thu Apr 22, 2004 2:23 pm    Post subject: Reply with quote

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... Smile
_________________
[ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial!
Back to top
View user's profile Send private message
jules
Professional Member
Professional Member


Joined: 14 Sep 2001
Posts: 1043
Location: Cumbria, UK

PostPosted: Thu Apr 22, 2004 4:33 pm    Post subject: Reply with quote

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


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
View user's profile Send private message Visit poster's website
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Fri Apr 23, 2004 1:11 pm    Post subject: Reply with quote

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

Cheers, Mac Smile

[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... Embarassed

_________________
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
View user's profile Send private message Send e-mail
Serge
Professional Member
Professional Member


Joined: 04 Mar 2002
Posts: 1480
Location: Australia

PostPosted: Sat Apr 24, 2004 12:34 am    Post subject: Reply with quote

hi mac,

hum...have you tried single quotes...jules mentioned using single quotes while your example showed double quotes Smile

serge

_________________
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Sat Apr 24, 2004 12:38 pm    Post subject: Reply with quote

Hey serge,

Of course it shows TRUE by adding one or more single
quotes - a single quote is a visible char. Wink

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 Smile

_________________
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
View user's profile Send private message Send e-mail
Skit3000
Admin Team


Joined: 11 May 2002
Posts: 2166
Location: The Netherlands

PostPosted: Sat Apr 24, 2004 1:20 pm    Post subject: Reply with quote

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

_________________
[ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial!
Back to top
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Sat Apr 24, 2004 3:15 pm    Post subject: Reply with quote

Hey Skit,

Ya missed the point entirely. Confused

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

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 Smile

_________________
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
View user's profile Send private message Send e-mail
jules
Professional Member
Professional Member


Joined: 14 Sep 2001
Posts: 1043
Location: Cumbria, UK

PostPosted: Sat Apr 24, 2004 5:28 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Sat Apr 24, 2004 6:27 pm    Post subject: Reply with quote

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

Cheers, Mac Smile

_________________
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
View user's profile Send private message Send e-mail
Skit3000
Admin Team


Joined: 11 May 2002
Posts: 2166
Location: The Netherlands

PostPosted: Sun Apr 25, 2004 11:22 am    Post subject: Reply with quote

I think people might see it as a bug and search for it here, so I think it's best to let this topic stay here... Smile
_________________
[ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial!
Back to top
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Sun Apr 25, 2004 2:18 pm    Post subject: Reply with quote

Thanks Skit. Wink

Cheers, Mac Smile

_________________
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
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Bug Reports All times are GMT
Page 1 of 1

 
Jump to:  
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

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group