Chris Bonnici's "Correct" Functions.
Shareware Version


List of topics:
1. Introduction
2. The Source
3. Installation
4. Running the Program
5. Shareware
6. Distribution and Disclaimer
7. Contacting the Author
8. Click here to download


Introduction
This library came into being as an extension of MWM005's tutorial. What better way to explain a function than to write your own version utilizing only the functions you have discussed?

Rather than copy the existing return codes of some M functions we have "corrected" them. The word correct is a very wobbly term to use as its definition varies from one person to another. In the authors opinion, the results returned by the items in this library are more consistent with some other language and that is how "correct" came into being.

Command
Alternative
Differences
$F
$$CBF^CBRTN1(STRING,SUBSTR,BEGIN)
This function returns the actual position of the found substring in string or 0 if not found or null.
$L
$$CBL^CBRTN1(STRING,SUBSTR,BEGIN,END)
The returns the correct count of substrings in string. Also allows the user to count the number of substrings within a section of string.
$E
$$CBE^CBRTN1(STRING,BEGIN,END)
Automatically swaps BEGIN and END if they don't follow.
Allows Programmer to specify a BEGIN larger than END and automatically adjust them.

The Source
CBRTN1(;Chris Bonnici's Intrinsic Function Enhanced v 1.0 - Chris Bonnici - August 1997
(;(c) 1997 Chris Bonnici - All rights reserved
(;You may freely to distribute this library of routines as long as:
(;  1. You provide the entire archive WITHOUT ANY MODIFICATIONS
(;  2. You do not charge for the program
(;  3. You are using this program at your own risk
(;
(; Chris Bonnici: email: chrisb@4u.net
(; URL: http://www.geocities.com/SiliconValley/7041/ (latest version of this library).
(;
(; There is no charge if you are using this library for personal purposes although an e-mail is appreciated.
(; If this library is used within a commercial environment a registration fee of $15 is mandatory after the 30 day trial period.
(; Any income earned from this program will be used to initIate some projects in M Web Magazine at http://www.mcenter.com/mwm-us/
(;
(; Thanks for your support
(;
(;Chris Bonnici's $F - Returns 'Correct' position match (i.e. $F-1) plus returns 0 with null substring
CBF(STRING,SUBSTR,BEGIN)(N I,J,FOUND
(Q:'$D(SUBSTR)!(SUBSTR="") 0
(S:'$D(BEGIN) BEGIN=1
(S J=1,FOUND=0
(F I=BEGIN:1:$L(STRING) D  Q:FOUND
(.I $E(STRING,I)=$E(SUBSTR,J) S J=J+1 S:J>$L(SUBSTR) FOUND=1 Q
(.S J=1
(.Q
(Q:FOUND I   ;Return 'Correct' Position
(Q 0         ;Not Found
(;*** EOR ***
(;Returns correct count of substrings in string. Allows user to count within a substr
CBL(STRING,SUBSTR,BEGIN,END)(N COUNT,TEMP
(S:'$D(SUBSTR) SUBSTR=""
(Q:SUBSTR="" 0
(S:'$D(BEGIN) BEGIN=1
(S:'$D(END) END=$L(STRING)
(I BEGIN>END S TEMP=BEGIN,BEGIN=END,END=TEMP
(S STRING=$E(STRING,BEGIN,END),(BEGIN,COUNT)=0
CBL10(S BEGIN=$$CBF(STRING,SUBSTR,BEGIN) Q:BEGIN=0 COUNT
(S COUNT=COUNT+1,BEGIN=BEGIN+1
(G CBL10
(;*** EOR ***
(;Allows Programmer to specify a BEGIN larger than END
CBE(STRING,BEGIN,END)(N TEMP
(S:'$D(BEGIN) BEGIN=1
(I $D(END) D  Q $E(STRING,BEGIN,END)
(.I BEGIN>END S TEMP=BEGIN,BEGIN=END,END=TEMP
(.Q
(Q $E(STRING,BEGIN)
 
Installation
Open the ZIP archive.
Run your version of MSM
Using %RR, load the file CBRTN1.RTN into your development. 

Running the program.
Check out MWM005's M Tutorial section for information about the $ functions replaced.
From with your programs call $$xxx^CBRTN1, where xxx is the name of the function..

Shareware
CBRTN1 is a shareware program  and there is no charge if you are using this library for personal purposes although an e-mail is appreciated. If this program is used within a commercial environment a registration fee of $15 is mandatory after the 30 day trial period.

Any income earned from this program will be used to initiate some projects in M Web Magazine at http://www.mcenter.com/mwm-us/ so if you can pitch in you'll see your donation turned into something useful to the entire M community.

For corporate licensing please contact the author.

Distribution and Disclaimer
CBRTN1 may be distributed by any means, provided that the original files as supplied by the author remain intact. This program may be placed on any archive or BBS system.

If used in a commercial environment, the program must be registered if used  beyond the evaluation period.

This program can not be bundled with any commercial package or distributed by itself (i.e. separate from the rest of the archive) without express written permission from the author.

The author of this program accepts no responsibility for damages resulting from the use of this product and makes no warranty or representation, either express or implied, including but not limited to, any implied warranty of merchantability or fitness for a particular purpose. This software is provided "AS IS", and you, its user, assume all risks when using it.

By installing this program you implicitly agree to the rules outlined above.

Contacting the Author
The latest version of this program can always be downloaded from Chris Bonnici's M Page http://www.geocities.com/SiliconValley/7041). Choose from the index Download M. 

Electronically, Chris Bonnici can be reached at chrisb@4u.net. 

Comments and suggestions are welcome.
