pkatanic
30th September 2015, 15:21
Hello,
I'm studying how to use function str.replace.
Example from Guide
str.replace(source, "dog", "cat", target)
| target now is allocated and contains "the quick brown fox jumps over the lazy cat"
target.len = len(target)
| target.len = 43
But I have array from database which I have to change. How to achieve that?
this is piece of code where I change data.(tfcmg301.orig(1;len(tfcmg301.orig)-4) = tfcmg301.orig(5;len(tfcmg301.orig))) My question is how to create array from database (tfcmg301) and using str.replace change data.
bhushanchanda
30th September 2015, 15:35
Hi,
You don't need str.replace() in this case. It can be done without that. For example ,following snippet works for 5 rows -
table ttfcmg301
extern domain tffst.clds orig.array(5)
function main()
{
long i
i = 1
select tfcmg301.*
from tfcmg301
as set with 5 rows
selectdo
tfcmg301.orig(1;len(tfcmg301.orig)-4) = tfcmg301.orig(5;len(tfcmg301.orig))
orig.array(1,i) = tfcmg301.orig
i = i + 1
endselect
}
And the array elements can now be accessed using - orig.array(1,1), orig.array(1,2) ...
mark_h
30th September 2015, 15:48
Interesting - never seen this function before. From looking at the sample and the code you posted it appears you truncate 4 characters off of tfcmg301.orig. Technically the tfcmg301.orig is a string field so it is an array. So I would think the following would work:
str.replace(tfcmg301.orig, "dog", "cat", tfcmg301.orig)
I would think it would take the string tfcmg301.orig, look for dog, replace with cat, then store back into tfcmg301.orig.
Just a swag from what I see in your post. Then again maybe I did not get what you were asking.
bhushanchanda
30th September 2015, 15:51
Mark,
Correct. It's an added tools function. Here is the full documentation. :)
str.replace()
Syntax:
function void str.replace (const string string$, const string oldstr$, const string newstr$, ref string result$)
Description
Returns as an output argument a (null terminated) copy of string$ that will have all instances of oldstr$ replaced with newstr$.
The result$ string will be resized as required, which ensures that in result$ all occurrences of oldstr$ have been replaced. In order to be able to resize result$, the variable passed must be declared as a based string. Use len() to know the new number of characters in result$.
Arguments
const string string$ a string
const string oldstr$ the part to replace
const string newstr$ the part to replace oldstr$ with
ref string result$ a copy of string$, with all instances of oldstr$ replaced with newstr$
Context
This function is implemented in the 4GL Tools and can be used in all script types. This function is available from TIV level 1700.
Preconditions
In case one of the variables passed to string$, oldstr$, or newstr$ is declared as a multibyte string, then the variable passed to result$ must be declared as a multibyte string as well.
The variable passed to the result$ parameter must be declared as a based string.
Example
string source(50)
string target(1) based
long target.len
| 1 2 3 4 5
| pos 12345678901234567890123456789012345678901234567890
source = "the quick brown fox jumps over the lazy dog"
| target is not yet allocated...
str.replace(source, "dog", "cat", target)
| target now is allocated and contains "the quick brown fox jumps over the lazy cat"
target.len = len(target)
| target.len = 43
str.replace(source, "the", "a", target)
| target is shrinked and now contains "a quick brown fox jumps over a lazy dog"
target.len = len(target)
| target.len = 39
str.replace(source, "o", "O", target)
| target is not resized and now contains "the quick brOwn fOx jumps Over the lazy dOg"
target.len = len(target)
| target.len = 43
str.replace(source, " ", " ", target)
| target is enlarged and now contains "the quick brown fox jumps over the lazy dog"
target.len = len(target)
| target.len = 51
str.replace(source, "the", "", target)
| this removes all instances of "the"
| target is shrinked and now contains "quick brown fox jumps over lazy dog"
target.len = len(target)
| target.len = 35
str.replace(source, "", "the", target)
| nothing is replaced
| target is resized to the size of source and now
| contains "the quick brown fox jumps over the lazy dog"
target.len = len(target)
| target.len = 43
string nonbased(100)
str.replace(source, " ", " ", nonbased)
| this results in an assert message, as string 'nonbased' should have been declared as based
mark_h
30th September 2015, 16:32
Is this another LN goodie or is there a library I have to include for 4c4? It would not compile on our ancient 4c4 system.
bhushanchanda
30th September 2015, 16:36
Haha. Unfortunately, its a Goodie!
A Laziness Add-on!! :D
mark_h
30th September 2015, 16:57
Figures - I could have removed several libraries or functions we use in the system. Oh well.
bhushanchanda
30th September 2015, 22:59
Figures - I could have removed several libraries or functions we use in the system. Oh well.
No fun in shortcuts Mark! "And well, app_start() is depreciated in the latest versions" Ancients still Rock! :)
pkatanic
1st October 2015, 15:58
Thank you gentlemans, you help me a lot!