String Operations

From BR Wiki
Revision as of 11:23, 9 January 2012 by Mikhail.zheleznov (talk | contribs) (edit)
Jump to navigation Jump to search

BR has one of the most powerful and easy-to-use set of methods for string manipulation in the industry. Consider the descriptions of these different methods below:

Concatenate

String concatenation is the operation of joining two or more character strings end-to-end. For example, the strings "snow" and "ball" may be concatenated to give "snowball".

In BR, the operator responsible for string concatenation is the ampersand &. So, the "snowball" example above may be carried out as follows:

00010 let string1$ = "snow"
00020 let string2$ = "ball"
00030 let result$ = string1$ & string2$
00040 print result$ ! this will print snowball

You may concatenate as many strings as you like. Note that if you are storing the concatenated result into a string variable, then this variable needs to be dimensioned long enough to fit the combined length of all the concatenated strings. Consider the following example:

00010 let string1$ = "snow"
00020 let string2$ = "ball"
00030 let string3$ = " effect of concatenating many strings"
00040 let result$ = string1$ & string2$ & string3$ ! this will result in error

The above example results in an error, because the default length of the string result$ is 18 characters, as any BR string. The combined length of string1$, string2$, and string3$ is 45 characters. Note that the error does not result from concatenating string1$ & string2$ & string3$. The error occurs when we try to assign a 45 character value to an 18 character string result$. In order to correct the error, we need to dimension result$ to at least 45 characters or more. Below is the corrected example:

00005 dim result$*45
00010 let string1$ = "snow"
00020 let string2$ = "ball"
00030 let string3$ = " effect of concatenating many strings"
00040 let result$ = string1$ & string2$ & string3$ ! this will result in error

Advanced

For increased speed of program execution, and shorter syntax you may incorporate the following methods.

Appending

To append string2$ to string1$ means to join string2$ to the end of string1$.

To append to the end of a String you should (for maximum speed of code execution) use

X$(inf:0)="append this to end"

OR

X$(inf:inf)="append this to the end"

Here, inf denotes infinity.

So X$(inf:inf) means "the substring of X$ starting at infinity". This is particularly useful when you don't know how long your string is and do not want to calculate its length.

see also: prepend


Prepending

To prepend string1$ to string2$ means to join string1$ to the beginning of string2$.

To append to the beginning of a string you should use

X$(0:0)="append this to front"

or alternately

X$(1:0)="append this to front"

For example,

00010 dim result$*255 ! dimension long enough to fit the result
00020 let result$ = " and this is the end"
00030 let string_to_prepend$ = "this is the front"
00040 let result$(0:0) = string_to_prepend$
00050 print result$

The output of the program above will be:

this is the front and this is the end



Prepend

To prepend string1$ to string2$ means to join string1$ to the beginning of string2$.

To append to the beginning of a string you should use

X$(0:0)="append this to front"

or alternately

X$(1:0)="append this to front"

For example,

00010 dim result$*255 ! dimension long enough to fit the result
00020 let result$ = " and this is the end"
00030 let string_to_prepend$ = "this is the front"
00040 let result$(0:0) = string_to_prepend$
00050 print result$

The output of the program above will be:

this is the front and this is the end


Append

To append string2$ to string1$ means to join string2$ to the end of string1$.

To append to the end of a String you should (for maximum speed of code execution) use

X$(inf:0)="append this to end"

OR

X$(inf:inf)="append this to the end"

Here, inf denotes infinity.

So X$(inf:inf) means "the substring of X$ starting at infinity". This is particularly useful when you don't know how long your string is and do not want to calculate its length.

see also: prepend


Replace

For disambiguation purposes, see also the Replace Parameter for file operations.

The Replace (REP) command replaces the contents of a specified file with the program lines currently in memory.

If no file name is specified for a REPLACE command, Business Rules now uses the file's complete original name (including the extension) for the file name. Previously, it would replace any existing extension with an extension of .BR.

CAUTION

The REPLACE command only saves a file in source format when one of two conditions is true:

1) When the file name extension is .BRS or
2) If the SOURCE (SO) parameter is used when the REPLACE command is specified.

e.g: For example, if the file ABC.XX were loaded from source (LOAD ABC.XX,SO), specifying REPLACE without a file name and without the SOURCE parameter would cause the file to be replaced as a program file rather than as source file.

Comments and Examples

The REPLACE command is most useful during program development; it allows you to save the updated version of a loaded program.

You must end program execution before using the REPLACE command. If it is necessary to save a program without ending it, you should use a LIST command in the form LIST >tempfile, then LOAD the listed-to file from source (see LIST and LOAD for more information)

REPLACE leaves program lines in memory when it saves them to a file.

REPLACE can send lines only to a file that already exists. The system overwrites the file's current contents with these lines. If you wish to send program lines to a new file, you must use either the SAVE or LIST commands.

The following example replaces, in display format, the file TEST.BRS with the lines currently in memory:

REPLACE B:TEST SOURCE

Syntax

REPLACE [<file name>] [{OBJECT|SOURCE}]

Defaults

  1. Replace the current file.
  2. Replace both source and object code using the default extension .BR.

Parameters

The optional "file name" parameter specifies the file to be replaced. The system appends the extensions .BR or .BRO (according to the parameters) when you do not specify an extension or include a period with the file name.

OBJECT saves the executable portion of the program; source information is omitted. Files which are saved in object code only are protected from alteration; the source code cannot be recovered from these files. The system appends the extension .BRO when you do not specify an extension or include a period with the file name.
SOURCE replaces the file in display (ASCII) format. If you do not specify an extension or include a period in the file name, Business Rules the extension .BRS.
 Example: replace MyProgram.wbo object

Technical Considerations

1) Unless you specify a file name extension in the file name, or include a period at the end of the file name, the system appends a default extension when you use REPLACE. With the following command, the system saves the lines in memory to a file that becomes NAME.BR:
REPLACE NAME

But with the following command (which includes a period), the system replaces the file as NAME, with no extension:

REPLACE NAME.
2) In contrast to the System/23, Business Rules removes the impact of deleted DIM statements on a program before the program is stored with a SAVE or REPLACE command.
3) In contrast to the System/23, Business Rules variables that are no longer referenced in a program before SAVE or REPLACE commands are executed. This eliminates the need for reloading a program from source and saving it in order to reclaim space from unreferenced variables.
4) BR programs being replaced now FREE the original file, after creating the BAK copy, as opposed to DROPping them before writing the replacement program edition. This causes operating systems with salvage turned on to save a copy of the original program.
5) Programs are copied to a .BAK file before replacing them. This makes them recoverable from a failed Replace operation (out of space or corrupted file), provided another Replace command is NOT issued before attempting recovery. OPTION 26 suppresses the creation of .BAK files.

See Also

Name=


Insert


Delete

{{:Delete}