Bxn
(Getting the hang of it)
2002-06-05 01:30 PM
Instr() Behaviour

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,


ShawnAdministrator
(KiX Supporter)
2002-06-05 02:30 PM
Re: Instr() Behaviour

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


Richard H.Administrator
(KiX Supporter)
2002-06-05 03:06 PM
Re: Instr() Behaviour

Agreed, it is a bug and should return zero.

Bxn
(Getting the hang of it)
2002-06-06 02:55 PM
Re: Instr() Behaviour

Second Point, as I'm a junior member :

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


ShawnAdministrator
(KiX Supporter)
2002-06-06 03:20 PM
Re: Instr() Behaviour

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


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

Thanks For The Advice ...
I'll Try The First Solution, 'Cause I Don't Know How To Manage The Second ! [Confused]


Sealeopard
(KiX Master)
2002-06-06 05:05 PM
Re: Instr() Behaviour

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.

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

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]


Richard H.Administrator
(KiX Supporter)
2002-06-06 05:31 PM
Re: Instr() Behaviour

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]


ShawnAdministrator
(KiX Supporter)
2002-06-06 05:45 PM
Re: Instr() Behaviour

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 ]


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

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]


ShawnAdministrator
(KiX Supporter)
2002-06-06 06:17 PM
Re: Instr() Behaviour

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.

Bxn
(Getting the hang of it)
2002-06-06 09:44 PM
Re: Instr() Behaviour

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 !


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

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


ShawnAdministrator
(KiX Supporter)
2002-06-06 10:32 PM
Re: Instr() Behaviour

Bxn, my gut feeling - is probably what your gut feeling is - that it can't be done - and I think your right ! Anybody ?

Richard H.Administrator
(KiX Supporter)
2002-06-07 09:11 AM
Re: Instr() Behaviour

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.


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

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 ]


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

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 ?


LonkeroAdministrator
(KiX Master Guru)
2002-06-11 11:56 AM
Re: Instr() Behaviour

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


Richard H.Administrator
(KiX Supporter)
2002-06-11 01:16 PM
Re: Instr() Behaviour

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.


Bxn
(Getting the hang of it)
2002-06-11 02:38 PM
Re: Instr() Behaviour

Still Agree ...

I suggest nevertheless and with my greatest respectful humility [Hum, this guy seems too much polite to be honest ...] , you change in your last example script :

$asFormatDisk[$FRENCH]="Composer le disque [O/N]"

With

$asFormatDisk[$FRENCH]="Formater le disque [O/N]"
[Wink] [Wink] [Wink]

And perhaps would I have proposed [O/N/P], with "P" standing for "Peut-être ..." [Big Grin]

But what a stupid thing I've made when posting this topic to SUGGESTIONS and then to BETA ! [Frown]

D'you have comments about my last reply in BETA :

quote:
And if we read (again !) the Doc Page 19, we see :
"Scripts can contain any character, except the /0 (NULL) ..."
So ...