RE: match() (not short, he he)
- Posted by Al Getz <Xaxo at aol.com> May 27, 2003
- 419 views
>Kat wrote: > >Yes, strangely enough, i have other ideas. > >i'd prefer > >match('a',"something") > >to not fail because of the alledgedly abhorent reason that 'a' isn't a >sequence. >It's too late to change, but i'd prefer > >match('a',"kat") > >to return 2, since the 'a' in "kat" is an atom. And > >match("b",{"a",'b',"b","c"}) >to return 3, because the third element "b" in the second parameter of >match >is a sequence, and the 2nd parameter isn't. I haveto test parms in >match() for >that first parm being a sequence all the time too. > >Kat > Hello again Kat, I dont think any of your ideas have been strange, rather, i think they hint on what all Euphoria users want in the language in one form or another. I dont think i made it quite that clear why i was saying match("b",{"a","b","c"}) should return 0 and not the number 3 though. The main reason is that match now returns a single number, which is a single dimensional quantity. The reason it has to be single dimensional is because match works on only one level of the sequence at a time. The level it searches in the second sequence is always the same as the level of the first sequence. It has to be this way, or else we wouldnt know where it was looking, and the number it returned would be meaningless to us. A simple example: match("b",{"abc"}) match("b",{"a","b","c"}) match("b",{"ab","c","d"}) match("b",{{"a","b","c"}}) These cant all return the number 2, can they? To find out, all we have to do is try using the proposed new results in a program and see what happens. In the following code, all i have done is forced match to return the number 2 for every case which i 'think' you want to see the number 2 returned. I did this simply by setting i=2 AFTER the match calls. By inspecting these results below, you will see that there is only one case where match truely returns the number 2, and it does appear to be correct. Let's try these one at a time and see... [1] s="abc" i=match("b",s) --returns 2 i=2 ?s[i] --prints >98< --works fine! [2] s={"a","b","c"} i=match("b",s) --returns 0 i=2 ?s[i] --prints >{98}< --is this correct? [3] s={"ab","c","b"} i=match("b",s) --returns 0 i=2 ?s[i] --prints >{99}< --this is clearly incorrect! [4] s={{"a","b","c"}} i=match("b",s) --returns 0 i=2 ?s[i] --subscript value 2 is out of bounds, reading from a sequence of length 1 Nobody seems to be saying that #1 above isnt correct, and that is the only one that works correctly, really. match returns 2 anyway. In #2, match returns 0 and when we force it to be a 2 we end up printing the correct number, 98, but it's inside of a sequence. Arguably, this may be interpreted to be correct in some cases, but in #3, when we apply the same idea, we get a totally incorrect result, as now we end up printing the number 99 inside a sequence, which is totally wrong. We end up printing the 'c' instead of what we were really looking for, the 'b'. in #4, we get really bad results, but again it is arguable that ?s[i] with i=0 would have done the same thing. This says that our code has to be able to handle the possibility of match returning a zero, but we still havent found the "b" nested inside of s in this example, so this fails also. The reason why some of these tests fail and some return incorrect results is because match, right now, only returns a single number. If match could return a sequence, this would change and what you want to do would work. Repeating #4 allowing match to return a sequence: [4r] s={{"a","b","c"}} i=match("b",s) --returns 0 ss={1,2,1} --'force' match to return sequence --now let's use this sequence to try to print the b: ?s[ss[1]][ss[2]][ss[3]] --prints >98< --yeahhh! Finally, we found the correct value within {{"a","b","c"}} but it took three dimensions to do it. Granted this would work, but match would have to be seriously updated Also, once found, it would be hard as heck to find the next occurance of "b" wouldnt it? Try that match() would also have to be updated to accept a starting point in the sequence, something i have always wanted in "find()". That's another story though -------> All i was asking for is that match to return a zero when the first sequence is null "" (exactly) with no regard for the second sequence. Without this, we can never use the following line in our code: while match(s1,s2) do for fear that s1 might become the null sequence at some point, as in: while match(s1,s2) do --reduce length of s1 and do other stuff end while Take care for now, Al