Daily Archives: February 11, 2014
Harbour All Functions – Q
XToC()
XToC()
Convert an expression to character type string
Syntax
XToC( <expValue> ) --> cValue
Arguments
<expValue> Designate an expression of some of the following data type: NUMBER, CHARACTER, DATE, LOGICAL.
Returns
XToC() return a string with the representation of data type of expValue.
Description
At first glance, the XTOC() function does not appear to accomplish anything that cannot be done with the other functions. However, the advantage lies in the fact that you do not have to pay attention to the input data types. For example, you can convert the data in every field within a loop into a string. You could then link these into a longer string and use them to index or for comparisons. Each data type always returns a string with a particular fixed length:
Each data type always returns a string with a particular fixed length: ---------------------------------------------------------- Data Type Result Length Similar function ---------------- --------- ------------------------------- Numeric 8 FTOC() Logical 1 Date 8 DTOS() String Unchanged --------------- --------- -------------------------------
Examples
. In the case of logical parameters, XTOC() works like LTOC(): ? XTOC(.T.) // "T" ? XTOC(.F.) // "F" . Numeric values always return an 8-byte string: ? XTOC(0) // Length 8 ? XTOC(9.9) // Ditto ? XTOC(-9.9) // Ditto ? XTOC(99) // Ditto ? XTOC(-99) // Ditto . A string returns the same string: ? XTOC("123ABCabc") // "123ABCabc" . A date returns the ANSI date: ? XTOC(CTOD("12/31/99")) // "19991231" ? XTOC(CTOD("01/01/00") // "19000101" . An empty or false date returns an empty string rather than a null string: ? XTOC(CTOD(" / / ") // " " ? XTOC(CTOD("77/77/77") // " " . Show a function where all the fields in a database are combined into one string. This way, you can do a complete comparison of the two data strings: // All fields in a record combined into one string FUNCTION STRINGREC PRIVATE nI, nFieldNo, cField, cStringRec cStringRec := "" nFieldNo := FCOUNT() // Number of fields FOR nI = 1 to nFieldNo cField := FIELD(nI) // Field name cStringRec := cStringRec + XTOC(&cField) NEXT nI RETURN (cStringRec)
Platforms
All
Files
Source is misc1.c, library is libct.
Seealso
NTOC(), FTOC()
WordToChar()
WordToChar()
Replace double with single characters
Syntax
WordToChar( <cDoubleCharacterSearchString>, <cString>, <cSingleCharacterReplaceString> ) -> cString
Arguments
<cDoubleCharacterSearchString> Designates multiple 2-byte sequences which are exchanged for corresponding individual characters from the <cSingleCharacter ReplaceString>.
<cString> Designates the character string within which 2-byte sequences are exchanged for individual characters.
<cSingleCharacterReplaceString> Designates a sequence of individual characters which correspond to 2-byte sequences in <cDoubleCharacterSearchString> for replacement within <cString>.
Returns
WordToChar() returns the modified character string.
Description
When you use SOUNDEX algorithms, sequences of two characters must often be exchanged for a single other character. WordToChar() makes this process extremely simple and quick. The function processes the <cString> in 1-byte steps. The behavior after exchanging a sequence for a character is determined by CSetAtMuPa(). If CSetAtMuPa is .F., the search for more sequences continues after the exchanged characters. If CSetAtMuPa is .T., the search for more sequences continues and includes the exchanged characters.
Notes
. The term “word” is not used here in the textual sense, but rather as it is used in assembler programming. A “word” consists of units of 16 bits, or more precisely, 2 bytes. . <cCharacterlist> can be shorter than <cDoubleCharacterList>. When this occurs, the function exchanges the sequences in <cDoubleCharacterList> that do not have corresponding sequences in <cCharacterlist> for the last sequence in <cDoubleCharacterList>.
Examples
. This example shows a simple replacement: ? WordToChar("aa", "Xaaaa", "a") // "Xaa" . This example shows WORDTOCHAR() in conjunction with CSetAtMuPa(): CSetAtMuPa(.F.) // Multi pass off ? WordToChar("aa", "Xaaaa", "a") // "Xaa" CSetAtMuPa(.T.) // Multi pass on ? WordToChar("aa", "Xaaaa", "a") // "Xa"
Compliance
WordToChar() is compatible with CT3’s WordToChar().
Platforms
All
Files
Source is wordtoch.c, library is libct.
Seealso
CSetAtMuPa(), CharRepl(), WordRepl()
Week()
Week()
Returns the calendar week a number
Syntax
Week( [<dDate>][, <lSWN>] ) -> nWeek
Argument
<dDate> Designates a date for which the week it lies in, is determined. The default is the system date.
<lSWN> Simple Week Number ( see below description )
Returns
WEEK() returns a sequential week number that designates in which week of the year <dDate> lies.
Description
Week() returns the calendar week a number. If no date is specified, the system date is used. An empty date via hb_SToD(“”) returns 0. The week of the year in which a day lies is information required by a number of technical financial calculations or wage accounting. This function determines this week and acknowledged all calendar rules. If <lSWN> is .T., Week() will calculate the “simple week number”, defined by – week #1 starts on January, 1st – week #(n+1) starts seven days after start of week #n If <lSWN> is .F. (default), the ISO8601 week number, defined by – weeks start on mondays – week #1 is the one that includes January, 4 will be calculated.
Note
. When no date is specified, then WEEK() uses the system date. An empty date leads to a 0 result.
Example
Determine the week for a date: dDate := CTOD("03/14/92") ? "This date is in week: ", WEEK(dDate)
Compliance
Week() is compatible with CT3’s Week(). <lSWN> is Harbour extention.
Platforms
All
Files
Source is dattime2.prg, library is libct.
Seealso
CDoW(), CToDoW(), DoW()
TabPack()
TabPack()
Pack fill characters to appropriate tab characters
Syntax
TabPack( <cString>, [<nTabWidth>], [<cFillChar|nFillChar>], [<cNewLineCharacters>], [<cTabChar|nTabChar>], [<lIgnore141>] ) -> cPackedString
Arguments
<cString> String to alter <nTabWidth>
<cFillChar|nFillChar> Width of tab or character to use to fill <cNewLineCharacters> string indicating new line, default is the string returned by hb_eol()
<cTabChar|nTabChar> character indicating a tab stop, default is chr(9)
<lIgnore141> .T., if the soft-CR used by MEMOEDIT() should be ignored as a newline indicator, default is .F. (functions uses chr(141))
Returns
TABPACK() returns the modified character string.
Description
This function does not simply exchange a simple sequence of the same characters for a tab; instead, it takes into account the true tab positions. If a space (or the <cCharacter|nCharacter>) is found at a tab position, and immediately preceding it there is at least one identical character, the function replaces this sequence (maximum <nTabWidth>) with a CHR(9). With tab characters, text can be packed by individual tab widths. As with every good text editor, there is no replacement with a tab within single or double leading characters.
Notes
. For the beginning of a line, the function takes into account the “normal” carriage return (CHR(13)/CHR(10)) as well as the high bit return (CHR(141)) used by MEMOEDIT(). Previously existing tab characters are also taken into account. . If another tab already exists, this tab can be removed with TABEXPAND(). The new tab can then be inserted using TABPACK().
Example
We have avoided replacing spaces with tabs in the following examples to keep the example legible. Tab width is 8 (default). "*" is exchanged for tab characters. ? TABPACK("AAAAAAA*", "*") // "AAAAAAA*" ? TABPACK("AAAAA***", "*") // "AAAAA" + CHR(9) ? TABPACK("AAAAA*****", "*") // "AAAAA" + CHR(9) + "**" crlf := CHR(13) + CHR(10) cText := "ABCD+" + crlf + "++---+++++" ? TABPACK(cText, 4, "+") // "ABCD+" + crlf + // "++---" + CHR(9) + "++"
Compliance
TabPack() is compatible with CT3’s TabPack(), but extended by Harbour; there are three new parameters for a better fine control of the function’s behaviour.
Platforms
All
Files
Source is tab.c, library is libct.
Seealso
TabExpand()
TabExpand()
TabExpand()
Replace tabulator control characters with fill characters
Syntax
TabExpand(<cString>, [<nTabWidth>], [<cFillChar|nFillChar>], [<cNewLineCharacters>], [<cTabChar|nTabChar>], [<lIgnore141>]) -> cExpandedString
Arguments
<cString> String to alter
<nTabWidth> Tab width
<cFillChar|nFillChar> Fill Character
<cNewLineCharacters> string indicating new line, default is the string returned by hb_eol()
<cTabChar|nTabChar> character indicating a tab stop, default is chr(9)
<lIgnore141> .T., if the soft-CR used by MEMOEDIT() should be ignored as a newline indicator, default is .F. (functions uses chr(141))
Returns
The modified <cString> is returned.
Description
The TabExpand() function replaces all tab characters (CHR(9)) in <cString> with the corresponding number of spaces or <cCharacter| nCharacter>. The <nTabWidth> parameter can be used to set varying tab widths.
Notes
. In the absence of the <nTabWidth> parameter, TabExpand() has a default tab width of 8. . The carriage return and line feed, where the high bit is set, are taken into consideration. . SETTABS() does not affect this function.
Examples
. TabExpand() only fills to the next tab position: ? TabExpand("-" +CHR(9) +"!") // "- !" ? TabExpand("----" +CHR(9) +"!") // "---- !" . You can fill with any character you like: ? TabExpand("-" +CHR(9) +"!", "+") // "-+++++++!" . The tab width can be selected: ? TabExpand("-" +CHR(9) +"!", 4) // "- !" ? TabExpand("----" +CHR(9) +"!", 8) // "---- !" ? TabExpand("----" +CHR(9) +"!", 8, "+") // "----++++!"
Tests
TabExpand("-"+chr(9)+"!") == "- !" TabExpand("----"+chr(9) +"!") == "---- !" TabExpand("-"+chr(9)+"!",, "+") == "-+++++++!" TabExpand("-"+chr(9)+ "!", 4) == "- !" TabExpand("----"+chr(9)+ "!", 8) == "---- !" TabExpand("----"+chr(9)+ "!", 8, "+") == "----++++!" TabExpand("-"+chr(9)+"!"+hb_eol()+"----"+chr(9)+ "!",, "+") == "-+++++++!"+hb_eol()+"----++++!"
Compliance
TabExpand() is compatible with CT3’s TabExpand(), but there are three new parameters for a better fine control of the function’s behaviour.
Platforms
All
Files
Source is tab.c, library is libct.
Seealso
TabPack()
StrSwap()
StrSwap()
Swap the contents of two strings
Syntax
StrSwap( <[@]cString1>, <[@]cString2> ) -> cString
Arguments
<cString1> [@] and <cString2> [@] Designate the two strings that are interchanged. At least one of the character strings must be passed by reference.
Returns
StrSwap() always returns a null string. This function only affects strings that have been passed by reference.
Description
StrSwap() interchanges the strings <cString1> and <cString2>. The exchange continues until all the characters in the shorter character string have been moved. The function terminates when the last character of the shortest string is exchanged.
Note
. The character strings are changed directly. Therefore, at least one of the strings must have been passed by reference to get a result.
Examples
. In this example, two strings are passed by reference: cStr1 := "1234567890" cStr2 := "ABCDEFGHIJKLM" ? StrSwap(@cStr1, @cStr2) // Return value: "" . The two strings subsequently contain these characters: ? cStr1 // "ABCDEFGHIJ" ? cStr2 // "1234567890KLM" . In this example, only one of the strings is passed by reference: cStr1 := "1234567890" cStr2 := "ABCDEFGHIJKLM" ? StrSwap(cStr1, @cStr2) // Return value: "" . Both strings subsequently contain the characters of the string passed by reference: ? cStr1 // "1234567890" ? cStr2 // "1234567890KLM"
Compliance
StrSwap() is compatible with CT3’s StrSwap().
Platforms
All
Files
Source is strswap.c, library is libct.
Seealso
Introduction
Quarter()
Quarter()
Returns a number equal to the quarter in which a date falls
Syntax
Quarter( [<dDate>] ) -> nQuarter
Argument
<dDate> Designates in which quarter the specified date lies. The default is the system date.
Returns
Quarter() returns a number that identifies the quarter in which <dDate> lies.
Description
Returns a number equal to the quarter in which ddate falls. If <ddate> is empty, the system date is employed.
Note
. When no date is specified, the function automatically uses the system date. An empty date returns 0 as a result.
Example
Compute a delivery date: ? "We will deliver in quarter ", Quarter(CTOD ("07/12/1991")), "1991"
Compliance
Quarter() is compatible with CT3’s Quarter().
Platforms
All
Files
Source is dattime2.prg, library is libct.
Seealso
BOQ(), EOQ()
REPLRIGHT()
REPLRIGHT()
Replace certain characters at the right of a string
Syntax
REPLRIGHT( <cString>, <cReplace|nReplace>, [<cSearch|nSearch>] ) -> cString
Arguments
<cString> [@] Designates the string that is processed.
<cReplace|nReplace> Designates the character that replaces the character in <cSearch|nSearch> at the end of the <cString>.
<cSearch|nSearch> Designates the character at the end of <cString> that is replaced by <cReplace|nReplace>. The default value is a space, CHR(32).
Returns
The processed <cString> is returned.
Description
REPLRIGHT() can be used to exchange all trailing characters in a string for any character choosen.
Note
. The return value of this function can be suppressed by implementing CSETREF() to save space in working memory.
Examples
. Replace the trailing spaces with dashes: ? REPLRIGHT("abcd ", "-") // "abcd--" . Replace the trailing dashes with spaces: ? REPLRIGHT("abcd--", " ", "-") // "abcd " . Replace only the trailing spaces: ? REPLRIGHT(" 1 ", "-") // " 1--"
Compliance
REPLRIGHT() is compatible with CT3’s REPLRIGHT().
Platforms
All
Files
Source is replace.c, library is libct.
Seealso
REPLALL(), REPLLEFT(), CSETREF(), Introduction