Page 1 of 1 1
Topic Options
#182017 - 2007-10-28 12:06 AM Is the variable empty?
_Andy_ Offline
Fresh Scripter

Registered: 2007-10-27
Posts: 17
Loc: Sydney, Australia
I have a little issue with checking if a variable is empty...

If I use the "get" command and just press "Enter" then the variable is not blank??? How can I get the code below to give the same result?

 Code:
break on
$= SetOption('Explicit','ON')
$= SetOption('WrapAtEOL','ON')
$= SetOption('NoVarsInStrings','ON')
$= SetTitle('Info...')

Dim $myvar
$myvar = ''

?
? 'Myvar: |' + $myvar + '|'
?
If $myvar = ''
  ? 'Empty variable'
Else
  ? 'Not so empty...'
Endif
?
? 'Length of myvar: ' + len($myvar)
?
?
? 'Enter a value for myvar: '
flushkb
get $myvar
?
? 'Myvar: |' + $myvar + '|'
?
If $myvar = ''
  ? 'Empty variable'
Else
  ? 'Not so empty...'
Endif
?
? 'Length of myvar: ' + len($myvar)
?
Get $


Regards,
Andy
_________________________
Regards,
Andy.


--Just shoot me, please.--

Top
#182018 - 2007-10-28 12:23 AM Re: Is the variable empty? [Re: _Andy_]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
A null string is not the same as empty.

Instead of If $myvar = ''

try

? VarTypeName($myvar)

and determine if the state matches one defined in the VarTypeName results table.

You can then set up your If...Else...Endif construct.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#182020 - 2007-10-28 12:48 AM Re: Is the variable empty? [Re: Howard Bullock]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
After testing, I think that KiXtart is behaving properly. It is your example that is causing your confusion. Let's examine the issue closer...


When your hit the Carriage Return, $myvar is not longer empty. It contains ascii character 13 (the CR).

The way you you output the text the text is printed
? 'Myvar: |'

Then you print ascii character 13 held in $myvar which returns the cursor to the front of the line. Then the last pipe character is printed over the "M" leaving you the text "|yvar: |".

The length($myvar) correctly prints 1.

You can see what character is in your variable by printing ASC($myvar).
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#182023 - 2007-10-28 04:19 AM Re: Is the variable empty? [Re: Howard Bullock]
_Andy_ Offline
Fresh Scripter

Registered: 2007-10-27
Posts: 17
Loc: Sydney, Australia
Thank you.
_________________________
Regards,
Andy.


--Just shoot me, please.--

Top
#182037 - 2007-10-29 03:10 AM Re: Is the variable empty? [Re: _Andy_]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Thinking about your script's usage of "GET" which captures a single keyboard input, I think you can simply check to see if you find the expected key stroke using a Select...Case...EndSelect construct and use a "Case 1" to trap any unexpected key press.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 764 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.051 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org