Page 1 of 3 123>
Topic Options
#78901 - 2002-06-06 05:08 PM Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
As I discussed with some Members in the Suggestions Forum, I'd like to report something which puzzle me for a long time :

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., or 0.

Am I Wrong ?

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

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Original thread here.

{EDIT}
Oops - fixed link.
{/EDIT}

[ 10 June 2002, 10:03: Message edited by: Richard Howarth ]

Top
#78903 - 2002-06-08 04:17 AM Re: Instr() Behaviour
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
cant get to the original thread...

I quess, for logical point of view it should be as it is.
_________________________
!

download KiXnet

Top
#78904 - 2002-06-08 04:21 AM Re: Instr() Behaviour
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Howard,
you managed to get an extra http:// in front of the link.

The correct link should be http://81.17.37.55/board/ultimatebb.php?ubb=get_topic;f=4;t=000222
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#78905 - 2002-06-08 04:35 AM Re: Instr() Behaviour
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
thanks les.
didn't bother to check it.

but now when I've red it I can say this ain't bug.

and about giving empty value...
if it is a control char and also the string to be check on, it should return correctly: "Match"
but if user puts something dynamically in to this string comparison and does not make any length check or error check, it is he himself who gets the blame.

so what ppl where really talking about was that when instr() does not have
if len() fail endif structure inbuild
it is a bug!

this is really weird thinging.
missing a feature is not a bug.
as a user of various m$ products, I can say it's vice versa.
_________________________
!

download KiXnet

Top
#78906 - 2002-06-08 04:48 AM Re: Instr() Behaviour
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I guess I'll have to toss in my 2 cents (CDN). Without getting into a zen debate... it is a bit of a paradox. True, if you search for nothing, you find nothing, hence the positive return but...
the positive return is the offset which is incorrect.

So, I believe it should not return a 1 but rather it should return a 0.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#78907 - 2002-06-08 05:39 PM Re: Instr() Behaviour
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
les, you are correct... somehow as it's thinked.

but, if you think of the string.
it start counting from 1
does the first char match.
with nothing everychar matches.
so. it gives the first occurance in string.
it is the current coding.

to fix it to zero in way that it checks first for char 0 which (obviously) is nothing.

but the returning value 0.
doesn't that inform that there is no string present in presence of another?

this is actually little bit tricky and even thou it isn't a bug, it might get newbies to not to use it.
but has it over the years?
it has been working for long time and like the way it is.
_________________________
!

download KiXnet

Top
#78908 - 2002-06-10 09:59 AM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Very Disapointed about these views, Lonkero ...
First of all my poor english probably prevented me to fully understand the very substance of your first post ... (20020608 04:35), but I guess it's largely discussing about the duty of a programmer to check the values of variables used in order to prevent a lot of confusing things. If it is so, I of course agree with that ... Be sure I check the numeric variable $X before evaluatin' 1/$X !
Concerning the second post, I think, you certainly noticed that in my first post, I suggested too that "nothing was in everything". Well, but I don't agree with your statement "with nothing everychar matches".
the term "matches" is to vague to be USEFUL in a programming language (Thanx to Richard Howarth to have remind me this premacy of 'useful' coding against 'beauty of mathematical concepts' ! [Wink] ).
I realized that if I accept an Empty String matches a "A", tomorrow, one programming language developper will furbish me with the super tricky function SuperTrickyGraphicalInstr(), which will return 1 when I evaluate SuperTrickyGraphicalInstr(";","."), just simply it found a dot in ";".
Unless very much documented, a fine and consistent function should only evaluate Strict Identity, not 'belonging'.
You also state "it start counting from 1", unless you are the developper of KiXtart, that's only one point of view.
I personnally have to suggest would someone want to reflect the "theorical fact" that empty is in everything, in my example with string "ABCD", it should check the empty space before the A and shout : "Match !" and return 0 (suggested also and again by Richard Howarth).

In terms of consistency, reliability and efficiency, you didn't convince me, but did you ever tried ?

Nevertheless, this debate could be opened for other programming languages (or self proclammed Prog. Lang. !) :
I have tried equivalent functions with Turbo Pascal 7 [Pos() function] and MS Excel [localized Cherche() function] ...
With my example, the first returns 0, the second returns 1 ...

Top
#78909 - 2002-06-10 09:28 PM Re: Instr() Behaviour
Jack Lothian Offline
MM club member
*****

Registered: 1999-10-22
Posts: 1169
Loc: Ottawa,Ontario, Canada
My 2 cents worth.

I think you guys are mixing two subject: usefulnees of the function & proper logic.

Logically if you search for nothing you will find what you are searching for. Furthermore since you found something it must be somewhere. Logically it was in the first place you looked. Logically I think the function is consistent. Consider the point if you search for a null string in a null string. They are equivalent in every sense and if you follow some of the logic given above you wouldn't find one in the other. Isn't this illogical.

(I am not sure but might this not be a variant of the Bertand Russell paradox concerning set theory. The implication of the theorem is some logic systems are not closed.)

But consistency is not everything, since multiple consistent logic system can be constructed. I think the thrust of Richard's comments is that in this case usefulness might be a better criteria than logic. I tend to agree with Richard.

[ 10 June 2002, 22:27: Message edited by: JackLothian ]
_________________________
Jack

Top
#78910 - 2002-06-10 10:24 PM Re: Instr() Behaviour
Jack Lothian Offline
MM club member
*****

Registered: 1999-10-22
Posts: 1169
Loc: Ottawa,Ontario, Canada
[Embarrassed] Oops
_________________________
Jack

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

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Dear Jack,

That's in substance what I said.

But you see it obviously lead to some debate among developpers.

Nevertheless, if you agree our PoV (or Howard's) you can't say the function is really consistent as provided.

You said :

"Logically if you search for nothing you will find what you are searching for."

Uh oh ! That was the meanin' of my first post : "Nothing is in Everything". But we are programmers, we don't "search for nothing", we search for the "null string" and as far as I know in a programming language the Null String is not 'nothing', it's not the 'no code' character.

As a programmer, I don't expect from a function to answer :
"Oh you lazy boy, you're lookin' for nothin' so here's my answer : you win, and first try !"
where first try would lead me to "A" or "Z" ...

"Furthermore since you found something it must be somewhere. Logically it was in the first place you looked. Logically I think the function is consistent."

I already have heard this (Hey Lonkero !) ... But why should the first place we look, be the number "1" ? Especially concerning the Null String, the first place we have to look, is before the first char., where there's nothing as we all agree ... Returnin' "1" is not consistent. "1" is the position of the "A", and even if we all agree the element "nothing" is in every set, I've never been told that the element "nothing" was in all element of the set ! That's, I think another common confusion.

"Consider the point if you search for a null string in a null string"
Yes, there is strict identity between these values, but what's the lenghth of the null string ? Would it really have sense to return "1" where length is "0" ?
So, I would like a returned value of -2.

"But consistency is not everything," sure, that's why I like Neural Networks so much.

"I think the thrust of Howard's comments is that in this case usefulness might be a better criteria than logic. I tend to agree with Howard"

So Do I, But His Rate Is Already At Max. [Big Grin]

Top
#78912 - 2002-06-12 12:55 AM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
And if we read (again !) the Doc Page 19, we see :
"Scripts can contain any character, except the /0 (NULL) ..."
So ...

Top
#78913 - 2002-06-11 02:22 PM Re: Instr() Behaviour
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
bxn...
is 0 length string same as if it contains null?
NOT.
it also depends of the language, but in many cases the null char needs to be added.

this is just for nothing, cause I tend to agree with howard too, but like the "moral"/"political" issue in this thread [Smile]
_________________________
!

download KiXnet

Top
#78914 - 2002-06-11 02:47 PM Re: Instr() Behaviour
Bxn Offline
Getting the hang of it

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
Lonkero,
You're right, I certainly go too far in this case ! [Wink]
And I think you're right too when noticing it's language dependent !

And Finally, I'm glad to see your PoV more accurately ...
[Smile]

Top
#78915 - 2002-06-11 03:26 PM Re: Instr() Behaviour
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Lonkero,
quote:
is 0 length string same as if it contains null?
NOT.

Well, to be pedantic yes it is. The null character Chr(0) terminates a string. Therefore a string is actually all the character you can see, plus a Chr(0) at the end that you can't.

The string "Hi!" is actually four characters long -
'H' 'i' '!' '\0'

The empty string "" is 1 character long -
'\0'

This is the core reason why you cannot easily manipulate binary data as strings.

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

Registered: 2002-06-05
Posts: 63
Loc: Alpha Centauri
That's what I'd learned from other languages, but after the affirmation of Lonkero, I was not sure KiXtart did the same ...
Glad to learn more and more !

Top
#78917 - 2002-06-11 06:08 PM Re: Instr() Behaviour
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
howard... yes. yes.
I can see it being that way in C/c++
but, it's not as it's have to be that way.

kix, as it's script-language, can determine pretty much what strings contain.

and if you try to add to the end of string manually the null character what happens?
cause that's allowed in c...

so I'm speaking, like this thread, theoretically, as ruud hasn't said (uttleast to my knowledge) what elements string contains other than viewable characters if any...
_________________________
!

download KiXnet

Top
#78918 - 2002-06-11 08:59 PM Re: Instr() Behaviour
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
This is a GREAT discussion!

I find myself more and more in agreement with the idea that:

SUBSTR($X,"") should always return 0.

As was mentioned before, the value returned is an index of the first instance of the 2nd string passed to the function. Even if using set theory causes us to say that the null set is in anything, the value indexed as 1 can never be equal to an empty string.

Brian

Top
#78919 - 2002-06-11 09:04 PM Re: Instr() Behaviour
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
yeah.
but zero neither is correct one.
it should error out or give negative value.

but when starts all things work theoretically work correctly in practise?
_________________________
!

download KiXnet

Top
#78920 - 2002-06-11 09:05 PM Re: Instr() Behaviour
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
How is zero incorrect? If the 2nd string (not empty) is not in the 1st string, then there is no index, either, so zero is returned. This would be exactly the same situation. Are you saying that if a string doesn't exist in the other string, we should assign that a negative value as well?

Brian

Top
Page 1 of 3 123>


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

Who's Online
0 registered and 304 anonymous users online.
Newest Members
Praveer, Ollero, rayyo, Foxlicht, DonChild
17742 Registered Users

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