#79983 - 2001-04-24 09:54 AM
Re: Make the length parameter in SUBSTR() optional...
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Agreed the solution could be more elegant.If a change to substr() was going to be made I'd strongly suggest keeping the three parameter format. The bugs I take longest to track down are those involving overloaded operators and optional function arguments. And spelling mistakes  I suggest that the change should allow a negative value as the third parameter (some BASICs support this IIRC). For example... code:
substr("NO BALL GAMES ALLOWED",4,-1) -> "BALL GAMES ALLOWED" substr("ADOSFILE.TXT",5,-5) -> "FILE"
You keep the three parameters so function prototype checking will still catch you if you forget one, and you extend the usefulness of the function. If you think the "9999" might be unclear in the current version, then:
code:
$EndOfString=9999 $string="This is my KiXtart string" SUBSTR($string, 9, $EndOfString) ?
------------------
Richard Howarth
|
|
Top
|
|
|
|
#79984 - 2001-04-24 01:00 PM
Re: Make the length parameter in SUBSTR() optional...
|
Anonymous
Anonymous
Unregistered
|
I have mixed feelings about optional parameters and have seen numerous problems caused by a simple typo, or a forgotten parameter, being accepted as valid in languages where such "features" exist. Debugging can certainly become more involved and problems can become more subtle.To keep things nice and simple perhaps the special case of zero for the length could be used to mean the rest of the string. It's certainly easy enough to type and read and would also provide backward compatability.
|
|
Top
|
|
|
|
#79985 - 2001-05-01 03:55 PM
Re: Make the length parameter in SUBSTR() optional...
|
Anonymous
Anonymous
Unregistered
|
May I suggest an easier way of getting the right hand part of a string as follows: SUBSTR("abcdefghijk",-4,2) which would return "hi" Basically if the start position is negative then it refers to the end of the string, not the beginning of the string (-1 refering to the last character).
|
|
Top
|
|
|
|
#79987 - 2001-05-03 12:48 AM
Re: Make the length parameter in SUBSTR() optional...
|
Anonymous
Anonymous
Unregistered
|
I really like the idea of being able to start from the right hand end and think it would be a very worthwhile addition. However, that isn't the issue with the original suggestion, which refers to situations where we just don't know how long the string is. For example, separating the fields in a comma separated line of undefined length. I always approach such a task by splitting off the left most part, one bit at a time.
|
|
Top
|
|
|
|
#79988 - 2001-05-02 01:54 PM
Re: Make the length parameter in SUBSTR() optional...
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Sounds good to me.To summarize: substr(STRING,START,LENGTH) Where:
code:
START > 0 = Start position from start of STRING (1=1st character) START < 0 = Start position from end of STRING (-1=Last character, -2=penultimate character) LENGTH > 0 = Number of characters to substring LENGTH < 0 = Substring end position (-1=Last character in string)
Examples:
code:
substr("A String",1,1) = "A" substr("A String",3,-1) = "String" substr("A String",3,-4) = "Str" substr("A String",-3,2) = "in" substr("A String",-4,-1) = "ring" substr("A String",-6,-4) = "Str"
------------------
Richard Howarth
|
|
Top
|
|
|
|
#79989 - 2001-05-11 03:13 AM
Re: Make the length parameter in SUBSTR() optional...
|
kholm
Korg Regular
   
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
If an optional parameter is omitted in the midle of a function, you have to add an extra comma, so why not ask Ruud to make a stricter syntaxchecking for UDF's, personally i would like that to be an option: Strict On/OffExample, Function DoSomeThing($a,Optional $b, Optional $c): This is legal(Strict Off, normal way of KiXtart): $Res = DoSomeThing($a,$b,$c) $Res = DoSomeThing($a,$b) $Res = DoSomeThing($a,,$c) $Res = DoSomeThing($a) Whith a stricter syntax checking it should read like this, to be accepted(Strict On):
$Res = DoSomeThing($a,$b,$c) $Res = DoSomeThing($a,$b,) $Res = DoSomeThing($a,,$c) $Res = DoSomeThing($a,,) I just found cj's idea so good, that i have made this for my UDF liblary: code:
Function SubString($String, $Start, Optional $Len) $SubString = "" If VarType($String) <> 8 Return EndIf If VarType($Start) <> 3 Return EndIf If VarType($Len) <> 3 $Len = Len($String) - $Start + 1 EndIf $SubString = SubStr($String, $Start, $Len) EndFunction
Erikrhowarth and Fuse, what do you think about this idea! (Of course Ruud would have to agree in making this option)
|
|
Top
|
|
|
|
#79991 - 2001-05-12 06:41 AM
Re: Make the length parameter in SUBSTR() optional...
|
Anonymous
Anonymous
Unregistered
|
Eric,Sorry but I can't see the value of having strict checking as an option. I think it would most likely lead to sloppy coding habits which would no doubt be more difficult to debug, especially if coming back to code that hasn't been worked on for some time. As for the two variations, the idea of having empty fields separated by commas is good because it is immediately clear that there is an optional field. Simply leaving fields out gives no clue that there may in fact be optional fields. Who can remember such details about all the functions we will collect over time, whether our own or somebody else's? Certainly not me. It's always interesting to compare the way various languages implement such things. In the case of Visual Basic many otherwise poor ideas actually work quite well because there are on screen hints and reminders as you type. Other languages with similar features don't work so well because you are constantly having to check the help files and/or manuals. Kixtart would of course fall into this later group. Needles to say, things get even worse when we are talking about user written commands and functions, which do not even appear in the manual.
|
|
Top
|
|
|
|
#79992 - 2001-05-12 07:28 PM
Re: Make the length parameter in SUBSTR() optional...
|
kholm
Korg Regular
   
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
Fuse/cjThe sloppy code is allowed by default, allowing minimal codesize. But when i made the suggestion i 'forgot' that KiXtart code is not compiled, the Strict On/Off would only be usefull during compile time, or if someone makes a sort of pre-compiler. So the Strict On/Off is not an issue to build into KiXtart itself, but a wish for a precompiler that will leave a welldocumented code. Function Add3($a,Optional $b,Optional $c)
; Strict code, always allowed $Res = Add3($a,$b,) $Res = Add3($a,,) ; Sloppy code, also allowed, this should not be allowed by a strict pre-compiler $Res = Add3($a,$b) $Res = Add3($a) I see this forum as a sort of brainstorming, so your posts just brought op another idea to me. If the syntaxchecking is not to deeply burried in the code, i would suggest that Ruud adds a PreKix.exe with the only purpose to check for parameters (missing)
Erik
|
|
Top
|
|
|
|
Moderator: Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 581 anonymous users online.
|
|
|