Mat2Str: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 25: Line 25:


See also [[Syntax#MAT2STR|details about quotes]].
See also [[Syntax#MAT2STR|details about quotes]].
===MAT2STR===
MAT2STR( MAT zzz$, str$ [, sep$ [, flags$]] )
Where flag$ is in the format:
[ quote-type ] [ :LTRM ] | [ :TRIM ] | [ :RTRM ]
Where quote-type can be Q, QUOTES, ('), or ("), case insensitive. Quote-type denotes that each element should be enclosed in quotation marks. The trim flags denote pre-processing of array elements and the leading colon is only present when quote-type is specified.
If Q or QUOTES is specified the BR automatically determines which quote type to apply as follows:
First the element is unpacked. That is, if it is contained in quotes, the quotes are stripped and embedded pairs are singled. Next the element is scanned left to right for either type of quote character (single or double). If a quote character is encountered the element is enclosed in the alternate quote type and embedded occurrences of that quote type are doubled. If no quote character is encountered then double quotes are applied.
====Examples====
Quote Type is Q or QUOTES
abcdef -> "abcdef"
abc'def -> "abc'def"
abc"def -> 'abc"def'
abc""def -> 'abc""def' embedded quotes are left intact when quotes are not active
'abcdef -> "'abcdef"
Quote Type is ' (quote type single)
abcdef -> 'abcdef'
<nowiki>'abcdef -> '''abcdef'</nowiki>  single quotes get doubled when embedded in single quotes
"abcdef -> '"abcdef'  leading double quote is treated normally
Quote type double mirrors quote type single.
MAT2STR and STR2MAT trim outside of quotes but not inside of quotes. Also MAT2STR always adds quotes when quotes are present in the data. 
When using MAT2STR on a 2 dimensional array, the first delimiter is used for individual elements and the second delimiter at the end of each row. This principle also applies to three to seven dimensions.
Example
Given the following two dimensional array zzz$ containing the values-
    1            2
    3            4
The following statements-
    10 Sep$(1)=","
    20 Sep$(2)=hex$("0D0A") ! CRLF
    30 MAT2STR( MAT zzz$, str$, MAT Sep$ )
    40 PRINT str$
Will produce-
    1,2
    3,4


<noinclude>
<noinclude>
[[Category:Internal Functions]]
[[Category:Internal Functions]]
</noinclude>
</noinclude>

Revision as of 02:13, 12 July 2015

Business Rules! 4.20 introduces the Mat2Str internal function which converts an array to a string

MAT2STR(<Mat Source$>, <Destination$>, [, <Delimiter$>])

The default Delimiter$ is CR on Linux or CRLF on Windows.

DLM$ can be used to dynamically redimensions the array.

The delimiter will be put after every entry from the array including the last entry. The delimiter can be "" which would simply concatenate the array.

Mat2Str performs the opposite action of Str2Mat

Example:

00010 dim resulting_String$*100, array$(3)
00020 let array$(1)="first"
00030 let array$(2)="second"
00040 let array$(3)="third"
00050 mat2str(mat array$,resulting_String$,"//")
00060 print resulting_String$

Output:

first//second//third

See also details about quotes.

MAT2STR

MAT2STR( MAT zzz$, str$ [, sep$ [, flags$]] )

Where flag$ is in the format:

[ quote-type ] [ :LTRM ] | [ :TRIM ] | [ :RTRM ]

Where quote-type can be Q, QUOTES, ('), or ("), case insensitive. Quote-type denotes that each element should be enclosed in quotation marks. The trim flags denote pre-processing of array elements and the leading colon is only present when quote-type is specified.

If Q or QUOTES is specified the BR automatically determines which quote type to apply as follows:

First the element is unpacked. That is, if it is contained in quotes, the quotes are stripped and embedded pairs are singled. Next the element is scanned left to right for either type of quote character (single or double). If a quote character is encountered the element is enclosed in the alternate quote type and embedded occurrences of that quote type are doubled. If no quote character is encountered then double quotes are applied.

Examples

Quote Type is Q or QUOTES

abcdef -> "abcdef"
abc'def -> "abc'def"
abc"def -> 'abc"def'
abc""def -> 'abc""def' embedded quotes are left intact when quotes are not active
'abcdef -> "'abcdef"

Quote Type is ' (quote type single)

abcdef -> 'abcdef'
'abcdef -> '''abcdef'   single quotes get doubled when embedded in single quotes
"abcdef -> '"abcdef'   leading double quote is treated normally

Quote type double mirrors quote type single.

MAT2STR and STR2MAT trim outside of quotes but not inside of quotes. Also MAT2STR always adds quotes when quotes are present in the data.

When using MAT2STR on a 2 dimensional array, the first delimiter is used for individual elements and the second delimiter at the end of each row. This principle also applies to three to seven dimensions.

Example Given the following two dimensional array zzz$ containing the values-

   1            2
   3            4

The following statements-

   10 Sep$(1)=","
   20 Sep$(2)=hex$("0D0A") ! CRLF
   30 MAT2STR( MAT zzz$, str$, MAT Sep$ )
   40 PRINT str$

Will produce-

   1,2
   3,4