Page 1 of 2 12>
Topic Options
#80873 - 2002-06-05 01:30 PM Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
First, thanx for having put KiXtart to such a high level.

Nevertheless, I'd like to suggest a little thing.

The following script :

$String1 = "ABCD"
$String2 = "A"
$String3 = ""
? Instr($String1,$String2)
? Instr($String1,$String3)

returns :

1
1

I sure understand the mathematical point of view that argues empty is in everything, but I would have preferred a returned value of -1 for eg., because it would have prevent to think the first character of $String3 is "A".

Cheers,

Top
#80874 - 2002-06-05 02:30 PM Re: Instr() Behaviour
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
You make a good point. Intuitively, would have expected instr() to return a zero (false) - but would be interesting to hear what others think. Sounds to me like something that should be fixed, yeah ?

-Shawn

Top
#80875 - 2002-06-05 03:06 PM Re: Instr() Behaviour
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Agreed, it is a bug and should return zero.
Top
#80876 - 2002-06-06 02:55 PM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Second Point, as I'm a junior member :

Was this Forum the best place to report what we suspect to be a bug ?

Top
#80877 - 2002-06-06 03:20 PM Re: Instr() Behaviour
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Your right, you shouldn't leave it here, suggest you post another message to the "Beta" forum - which would probably get Ruud's attention. Maybe provide a backward link to here.

-Shawn

Top
#80878 - 2002-06-06 04:58 PM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Thanks For The Advice ...
I'll Try The First Solution, 'Cause I Don't Know How To Manage The Second ! [Confused]

Top
#80879 - 2002-06-06 05:05 PM Re: Instr() Behaviour
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Cikc on the URL button below the textbox when posting a message. This will enable you to insert a functioning URL into your post. Or, type the BBS code yourself.
_________________________
There are two types of vessels, submarines and targets.

Top
#80880 - 2002-06-06 05:20 PM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Thanx 2 U 2 !
Another Thing :
I don't totally Agree With a Return Value Of Zero for the followin' reason :
In my mind, for this particuliar function, as a non zero and positive returned value represents the "Offset of the first character of string2 found in string1", a Zero returned value doesn't mean "False", but "NoWhere".
So, -1 would mean "EveryWhere". [Wink]

Top
#80881 - 2002-06-06 05:31 PM Re: Instr() Behaviour
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Hmm.

While your observation is possibly correct, it is not useful for coding.

Consider the situation where you get user input and want to validate it. I used this just recently in an example to check a users keypress.

code:
Get $cKey
If InStr("YN",$cKey)
"User typed a valid key" ?
Else
"Please only hit the Y or N key." ?
Fi

If your user managed to get a null entry returned, the value (-1) would equate to TRUE and give a false result.

Also, don't forget that the value returned is the start position of the first instance of the string, not every instance so an "everywhere" value is not correct, but the first time the null string appears is before the first character of the string, which is of course character position zero [Wink]

Top
#80882 - 2002-06-06 05:45 PM Re: Instr() Behaviour
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Got to agree with Richard on this one. Zen speech aside, I think that "empty is in nothing" ergo there is no "". But then agin - I tend to be a "glass is half empty" kinda guy anways. There also is no spoon - which might explain why I keep spilling soup on my collar ... Richard, could we borrow some of your wisdom over in this thread:

Is there a need for an ActiveX crypto Kix object?

I know what your probably already thinking about this security rat hole, but would truely like your thoughts on this subject.

-Shawn

[ 06 June 2002, 17:52: Message edited by: Shawn ]

Top
#80883 - 2002-06-06 05:54 PM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
You convinced me, as a -1 value could cause backwards incompatibility with existing code !
Nevertheless, I didn't succeed in feedin' a Get command with a null value ... Sure I need some clues !
But your remark applies more generally during evaluations of varz non necessarily typed from console.
I fear I must incline [Wink]

Top
#80884 - 2002-06-06 06:17 PM Re: Instr() Behaviour
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
A realworld example of feeding yourself a NULL string might be when reading a non-existent value from the registry, or from an INI file, and performing an instr() on that.
Top
#80885 - 2002-06-06 09:44 PM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
That was effectively in my mind in my previous post, Shawn !
I really appreciate all of your contributions and point of U (even philosophical 'course) guys !

Top
#80886 - 2002-06-06 10:23 PM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
I (only) now think I understand your last post, Shawn !
But the clues I was lookin' foa were about how to feed a GET Command with a Null String ...

Top
#80887 - 2002-06-06 10:32 PM Re: Instr() Behaviour
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Bxn, my gut feeling - is probably what your gut feeling is - that it can't be done - and I think your right ! Anybody ?
Top
#80888 - 2002-06-07 09:11 AM Re: Instr() Behaviour
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
On my (UK) keyboard typing "Ctrl+Numeric keypad key" results in a null key value being returned.

Another bug maybe?

You see, with KiXtart anything is possible.

Top
#80889 - 2002-06-10 05:02 PM Re: Instr() Behaviour
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
agree with you on this one, Bxn.

Instr("asdlfjasdl","") should return 0.

Since type is boolean, we can assume that 1 = yes and 0 = no.

It's like asking if something is a factor of something else.

If you ask if 5 is a factor of 500 you determine the answer yes (1).
If you ask if 3 is a factor of 500 you determine the answer no (0).

0 is never a factor of anything except 0,
(nul is also never a factor of anything except itself: nul * anything = nul.)
therefore:

Instr("asdfasdf","") should return 0
Instr("asdfkljd","a") should return 1
Instr("ldsfkals","t") should return 0
Instr("","a") should return 0
and
Instr("","") should return 1

.....
I realize that 0 isn't the same thing as nul, but this makes much more sense intuitively than does nul being included in every set.
.....
As far as coding is concerned, it would probably be easier to have a different return code for each situation:

Instr("asdfasdf","") returns -1
Instr("asdfkljd","a") returns 1
Instr("ldsfkals","t") returns 0
Instr("","") returns -2
Instr("","a") returns 0

Brian

P.S. I realize this doesn't conform to set theory. Generically, any set contains the empty set as a subset, which is the way the function works right now. even empty sets contain empty subsets. Basically the INSTR asks if the sequence of letters intersects another sequence of letters. The null sequence can be included before and/or after the given sequence...
The difference between the "set theory" version and how someone would probably want to use the INSTR in KiXtart is that the null set ("") may not actually be meant to define an empty set, but rather one that does not contain a specific sequence. As such, it may be possible to set a default value (i.e. CHR(10)) to pass to the second string in SUBSTR that is known not to be contained in the first string.

[ 10 June 2002, 17:31: Message edited by: BrianTX ]

Top
#80890 - 2002-06-10 05:54 PM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Thanx for contributin' ...
As you certainly read, I was initially a 'differential returned codes' partisan, until the intervention of Richard Howarth ...

I think that perhaps in all cases, the main point for KiXtart Father would be to accurately explain the behaviour of this function.

PS : Did you see there were other PoVs in the Beta Forum ?

Top
#80891 - 2002-06-11 11:56 AM Re: Instr() Behaviour
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
best practice would be not to do anything with it without doing it fully.
maybe setting error code or something, but just giving 0 on "" checks is not good funct.

this is just one vote to keep it as it is...
_________________________
!

download KiXnet

Top
#80892 - 2002-06-11 01:16 PM Re: Instr() Behaviour
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
No, I disagree.

InStr() is broken at the moment. Returning "1" for a null search is not valid - the character at position 1 is not a null string.

I can't think of any situation where you would want to return anything other than "0". If you want to check for a null string you would simply compare it, you would not check the return value from InStr().

Having InStr() return "0" is a useful and intuitive response. Having it return any other value means that you have to code around it unnecessarily. If returning "-1" or "-2" would make InStr() more useful I'd agree, but it doesn't.

Take the following example, a simple way to ask to format a disk in the local language :
code:
Dim $asYesOrNo[10]
Dim $asFormatDisk[10]

$ENGLISH=1
$FRENCH=2
$asYesOrNo[$ENGLISH]="YN"
$asYesOrNo[$FRENCH]="ON"
$asFormatDisk[$ENGLISH]="Format Disk [Y/N]"
$asFormatDisk[$FRENCH]="Composer le disque [O/N]"

$iLanguage=$FRENCH

Do
$asFormatDisk[$iLanguage]
Get $sResponse ?
$iResponse=InStr($asYesOrNo[$iLanguage],$sResponse)
Until $iResponse

If SubStr($asYesOrNo[$ENGLISH],$iResponse,1)="Y"
"Formatting disk..."
EndIf

If InStr() returned "0" for a null keypress this script would be fine, but currently it will return "1" for a keypress which will appear to be "Y" and will execute the format disk code.

"-1" is also true, and would have unexpected results.

When you use InStr() in a program you are specifically using it to test for a string being present. You will never use it to test if a string is null, or if a null string is present.

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.065 seconds in which 0.026 seconds were spent on a total of 14 queries. Zlib compression enabled.

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