Page 1 of 1 1
Topic Options
#79979 - 2001-04-23 01:40 AM Make the length parameter in SUBSTR() optional...
Happy Birthday cj Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
and make it default to 'the rest of the string'

eg:
$string="This is my KiXtart string"

I would like
SUBSTR($string, 9) to return "my KiXtart string" without having to put SUBSTR($string, 9, len($string)-9)

cj

Top
#79980 - 2001-04-24 12:37 AM Re: Make the length parameter in SUBSTR() optional...
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
If you use an improbably large value for the length it will have the effect you desire, so...
code:
$string="This is my KiXtart string"
SUBSTR($string, 9, 9999) ?

Will work fine.

------------------

Richard Howarth

Top
#79981 - 2001-04-24 12:39 AM Re: Make the length parameter in SUBSTR() optional...
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Oops sorry guys, double posted due to trigger happy finger

------------------

Richard Howarth

[This message has been edited by rhowarth (edited 23 April 2001).]

Top
#79982 - 2001-04-23 11:06 PM Re: Make the length parameter in SUBSTR() optional...
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
cj

Great idea!
I think that many of us makes a lot of unnessasary (timeconsuming ?) code when returning the 'rest' of a string.

Ofcourse we could use rhowarth's suggestion, but it would make the code more clear to read, if by default a 'missing' length parameter in SubStr() was equal to the 'rest' of the string.

Anyway it could make the size of the scripts smaller!

Erik

[This message has been edited by kholm (edited 23 April 2001).]

Top
#79983 - 2001-04-24 09:54 AM Re: Make the length parameter in SUBSTR() optional...
Richard H. Administrator Offline
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
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
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
#79986 - 2001-05-01 05:57 PM Re: Make the length parameter in SUBSTR() optional...
Happy Birthday cj Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
That reminds me of InStrRev in VBS, it is InStr counted from the back - perhaps we could have your negative number idea in INSTR also.

cj

Top
#79987 - 2001-05-03 12:48 AM Re: Make the length parameter in SUBSTR() optional...
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 Offline
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 Offline
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/Off

Example, 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


Erik

rhowarth and Fuse, what do you think about this idea! (Of course Ruud would have to agree in making this option)

Top
#79990 - 2001-05-11 11:15 AM Re: Make the length parameter in SUBSTR() optional...
Happy Birthday cj Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
That 'extra comma' is actually you specifying that this option is "" or null...

I think that it is correct to have the 'extra comma' just the way it is...

my 2c


cj

Top
#79991 - 2001-05-12 06:41 AM Re: Make the length parameter in SUBSTR() optional...
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 Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Fuse/cj

The 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
#79993 - 2001-05-13 08:41 AM Re: Make the length parameter in SUBSTR() optional...
Happy Birthday cj Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
Some context sensitive text editors (like TextPad) have Clip Libraries that can be used to remind you of the parts of a function. You click on SELECT and it puts
SELECT
CASE
CASE 1 ; else
ENDSELECT
In your code for you. This is good because it helps you remember that it is CASE 1 and you don't forget the END...


As for PreKiX.exe, MCA has written a number of scripts and programs that are great for checking for syntax errors, missing )'s etc... Goto the search page, select Scripts, enter 'kixtart - tool' in the search window and MCA in the user window. Then remove the 'kixtart - tool' and try that search in the other forums.


The latest versions of these great scripts is at http://home.wanadoo.nl/scripting.

cj

Top
#79994 - 2001-05-15 05:45 AM Re: Make the length parameter in SUBSTR() optional...
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
Dear KHolm, CJ,

The discussion about correctness of parameter usage we like very
much.
At the moment we think about an extension of syntax checking of
this part. We will think about the usage of minimum required
parameters and the maximum of it.

CJ, we read with interest your way of finding tools + scripts
on our site.
To reach a wanted tool or script it is only necessary to
select the script button from the main screen from
the http://kixtart.org . All published software is placed
on one page.
We don't like it very much when users must check a lot of pages
to see the current state of the site.
Another element is the direct connection with the relevant
kixtart topic. Simply you must press the light icon of the
software item.
At least there is a History of Site and Summary of Site
page.
Greetings.


btw: according to CJ with add those page to the signature part.


------------------
Site:

[This message has been edited by MCA (edited 15 May 2001).]

_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
#79995 - 2001-05-15 05:50 AM Re: Make the length parameter in SUBSTR() optional...
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
Signature were incorrect.

------------------
Site:

_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
Page 1 of 1 1


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

Who's Online
1 registered (Allen) and 581 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

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

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org