#80878 - 2002-06-06 04:58 PM
Re: Instr() Behaviour
|
Bxn
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 !
|
Top
|
|
|
|
#80880 - 2002-06-06 05:20 PM
Re: Instr() Behaviour
|
Bxn
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".
|
Top
|
|
|
|
#80881 - 2002-06-06 05:31 PM
Re: Instr() Behaviour
|
Richard H.
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
|
Top
|
|
|
|
#80882 - 2002-06-06 05:45 PM
Re: Instr() Behaviour
|
Shawn
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
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
|
Top
|
|
|
|
#80889 - 2002-06-10 05:02 PM
Re: Instr() Behaviour
|
BrianTX
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
|
|
|
|
#80892 - 2002-06-11 01:16 PM
Re: Instr() Behaviour
|
Richard H.
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
|
|
|
|
Moderator: Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
0 registered
and 248 anonymous users online.
|
|
|