Page 1 of 1 1
Topic Options
#48401 - 1999-10-27 08:48 AM SELECT CASE bug?
Anonymous
Unregistered


I've run into a problem with the SELECT CASE
structure when the variable in the CASE
statements is undefined. For instance take the following script (testcase.scr):

code:

SELECT
CASE $var="one"
$num=1
CASE $var="two"
$num=2
CASE $var="three"
$num=3
CASE 1
$num=0
ENDSELECT

? $num


When invoked as

kix32 testcase $var="two"

it works as expected with output of "2". But if $var is not defined the first CASE expression seems to come out TRUE as

kix32 testcase

results in output of "1".

This means that you have to be careful to explicitly check the CASE $var="" condition
as the first CASE of any SELECT CASE ENDSELECT construct.

Anyone any ideas what's going on here?

Regards

------------------
--
Gerry Mulvenna, Applications Development Manager, Computing Services
Heriot-Watt University, Edinburgh, Scotland, EH14 4AS
http://www.mulvenna.demon.co.uk/gerry/ G.J.Mulvenna@hw.ac.uk
Tel: +44-(0)131-451 3794 Fax: +44-(0)131-451 3261

Top
#48402 - 1999-10-28 07:47 PM Re: SELECT CASE bug?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Gerry...

I've had the same problem. For example if you do a len($var) and $var wasn't defined on the command line, you get a script error. I get around this with an IF check on the command line variable first, like this...

code:

if $var
SELECT
CASE $var="one"
$num=1
CASE $var="two"
$num=2
CASE $var="three"
$num=3
CASE 1
$num=0
ENDSELECT
else
$num = 0
endif
? $num

This doesn't answer your question, but I hope it helps. Maybe KIX needs a DEF($var) type function so one can check if the variable was assigned on the command line.

Shawn.

Top
#48403 - 1999-11-04 01:17 PM Re: SELECT CASE bug?
Anonymous
Unregistered


You are trying to deturmine the value of an undefined variable... to make sure global variables carry through subroutines, I always DIM or set ($V = "") them at the top of a script with comments on my intended use.
If the variable isn't defined yet, I believe the case would think like:
1. set $var = "one" (since it is a new var).
2. Did it succeed (in setting it to "one")?
3. If so, this case is true.
Hope this helps...
SteveH

Top
#48404 - 1999-11-05 05:17 AM Re: SELECT CASE bug?
Anonymous
Unregistered


Yes, having tried out a few more things with undefined variables in conditions, this is exactly what is happening. The $var="whatever" is taken as an assignment which returns TRUE if $var is undefined. This applies to IF statements as well as CASE. So the usual check that is made on command line variables to check they exist is not actually doing what we intuitively imagine.
code:

IF $var=""
? 'Usage: kix32 testcase $var="something"'
EXIT(1)
ENDIF


The $var="" is actually an assignment if $var is undefined so we could put anything in the quotes and it will still return true.

I've sent something about this unexpected behaviour to rkinput@microsoft.com in case the Kixtart developers want to review the behaviour of undefined variables in conditions.

I suppose it's the ambiguity of the = operator that is avoided in languages which have different assignment and equality operators (e.g. = and ==).

Regards

------------------
Gerry Mulvenna, Applications Development Manager, Computing Services
Heriot-Watt University, Edinburgh, Scotland, EH14 4AS
www.mulvenna.demon.co.uk/gerry/ G.J.Mulvenna@hw.ac.uk
Tel: +44-(0)131-451 3794 Fax: +44-(0)131-451 3261



Top
#48405 - 2000-12-15 10:01 AM Re: SELECT CASE bug?
Anonymous
Unregistered


While waiting for MS to add new functionality to check for undefined variables, I think this will do the work (at least with kix32 ver 3.62)

Here are some code doing various checks

code:
;$var="dummy"
$dmyvar1=$var+"x"
?" Testing undefined variable var + x (string operation):$dmyvar1."
$dmyvar2=$var+"9"
?" Testing undefined variable var + 9 (number operation):$dmyvar2."

$var="dummy"
$dmyvar3=$var+"x"
?" Testing defined variable var + x (string operation):$dmyvar3."
$dmyvar4=$var+"9"
?" Testing defined variable var + 9 (number operation):$dmyvar4."

; Check undefined variable (use the first dummy variable)
If $dmyvar1=0
?" Variable var is undefined!"
Else
?" Variable var is defined!"
EndIf


The supprice comes when you look at the output:

code:
 Testing undefined variable var + x (string operation):0.
Testing undefined variable var + 9 (number operation):9.
Testing defined variable var + x (string operation):dummyx.
Testing defined variable var + 9 (number operation):dummy9.
Variable var is undefined!

I have used the $dmyvar1 variant successfully, an hope this tip will help you as well!

Regards HF

------------------

Top
Page 1 of 1 1


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

Who's Online
0 registered and 1046 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.05 seconds in which 0.021 seconds were spent on a total of 12 queries. Zlib compression enabled.

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