Jump to content
daanvdn

recursive CF to remove semi-variable textstring

Recommended Posts

daanvdn

Hi,

 

I'm looking for a CF that allows me to remove every occurence of a semi-variable textstring in a field: The string always starts with "(ID" and ends with ")", but the text in between is of variable length.

 

examples of such strings are:

 

(IDCMANCRIW-1,I.42.2)

(IDCMANCRIW-1,II.32.6)

(IDCMANCRIW-1,IV.20.1)

 

 

Is there a quick and easy way to remove all such strings?

 

Thanks,

 

Daan, Belgium, Ghent

Share this post


Link to post
Share on other sites
Weetbicks

Function Name: DeleteText

Parameters: Value, StringStart, StringEnd, Pos, Start, End

 

This is a recursive function, when you initially call this function, you need to in the following way:

 

DeleteText ( Field ; "(ID" ; ")" ; 1 ; "" ; "" )

 

The pos/start/end parameters are used by the function itself to keep track of the substitution process.

 

This could be the coolest custom function I've ever built :)

 

Case (
Pos > Length ( Value ) ; Value ;

Let ([

ChangeMade = If ( not IsEmpty ( Start ) and not IsEmpty ( end ) ; 1 ; 0 ) ;
Value = If ( ChangeMade = 1 ; Substitute ( Value ; Middle ( Value ; Start ; End - Start + 1 ) ; "" ) ; Value ) ;
Pos = If ( ChangeMade = 1 ; Start ; Pos ) ;
Start = If ( ChangeMade = 1 ; "" ; Start ) ;
End = If ( ChangeMade = 1 ; "" ; End ) ;

// So we have now done our substitute @ the start,  now work out new start/end values for next round.
StartPosition = If ( Middle ( Value ; Pos ; 3 ) = StringStart and IsEmpty ( Start ) ; Pos ; Start ) ;
EndPosition = If ( Middle ( Value ; Pos ; 1 ) = StringEnd and not IsEmpty ( Start ) ; Pos ; End )

];

//Run the recursion again
DeleteText ( Value ; StringStart ; StringEnd ; Pos + 1 ; StartPosition ; EndPosition )

)
)

 

 

If you want an explanation of how it works let me know, otherwise have fun.

Share this post


Link to post
Share on other sites
daanvdn

Thanks! It works great, fantastic!

 

kind regards

 

Daan

Share this post


Link to post
Share on other sites
daanvdn

I've tried the function in a global text field.

It really works great if the field contains a normal amount of text.

However, when I use it for an extremely large text portion (about 370.000 characters or 180 pages) the functions results in "?".

Could it be that there is a limit to the number of characters this function or FM as whole can take?

 

Again, thanks in advance!

 

Daan

Share this post


Link to post
Share on other sites
Weetbicks

Unfortunately there is a limit to the number of times a recursive custom function can call itself (I think it is limited to the size of the stack that is generated using recursion) , and I think it might be 10,000. Sorry I didn't realise that it would be used on such large text fields.

 

A solution of sorts I guess is to work out a fairly large sized chunk of text that will work, work out how many characters is in that, and then pass just that amount to the custom function each time from your text block.

 

So you may need 3 or 4 calls, ie:

 

DeleteText ( Left ( Field ; 10000 ) ; ..... ) &

DeleteText ( Middle ( Field ; 10000 ; 10000 ) &

DeleteText ( Middle ( Field ; 20000 ; 10000 ) etc..

 

In fact, you could build another custom function whose sole purpose is to handle the passing of chunks of text to DeleteText and then pieceing them back together, this one would also be a recursive function... I don't have time to write it now cos I gotta get to work, but if you can't think it up I might have a crack later on.

Share this post


Link to post
Share on other sites
daanvdn

Hi Weetbix,

 

Thanks again for that fantastic function! Turns out I don't need to use it on huge text portions, so the problem I mentioned earlier is no longer an issue.

I've tried it on normal text portions and it works great! There is, however, one small problem: the function removes all but the last string occurence ... Is there some way this can be fixed? I've tried figuring out the syntax myself, but it's way too advanced for me...

 

Thanks again in advance!

 

Kind regards

 

Daan, Ghent, Belgium

Share this post


Link to post
Share on other sites
Weetbicks

are you able to provide a sample of the text that is causing this issue? cheers.

Share this post


Link to post
Share on other sites
daanvdn

An sample would be the text at the bottom (it's Middle English, but I assume that doesn't matter).

The settings of the function parameters are as follows:

 

DeleteVariableText ( Value ; StringStart ; StringEnd ; Pos ; Start ; End )

 

Value = the field containing the sample below

StringStart = "(CMANCRIW-2"

StringEnd = ")"

Pos = 1

Start=""

End=""

 

Thanks again!

________________________________________

 

Nam ich king richest. (CMANCRIW-2,II.291.771)

Nam ich hest icunned. (CMANCRIW-2,II.291.772)

nam ich weolie wisest. (CMANCRIW-2,II.291.773)

nam ich mon ne hendest. (CMANCRIW-2,II.292.774)

Nam ich þunge $freost. (CMANCRIW-2,II.292.775)

for swa me seið bi large mon. þe ne con naut edhalden he haueð . (CMANCRIW-2,II.292.776)

þe honden as mine beoð iþurlet. (CMANCRIW-2,II.292.777)

Nam ich alre þinge swetest. (CMANCRIW-2,II.292.778)

Þus alle þe reisuns hwi me ach to ȝeoue luue þu macht ifinden inme nomeliche. ȝef þu luuest chaste {TEXT:l_subpuncted} cleannesse. (CMANCRIW-2,II.292.779)

for nan mei luue me bute ha halde hire. (CMANCRIW-2,II.292.780)

ȝef þiluue nis naut to ȝeouen. Ach wult me bugge hire. buggen hire. (CMANCRIW-2,II.292.781)

hu; (CMANCRIW-2,II.292.782)

Share this post


Link to post
Share on other sites
Weetbicks

Hi daanvdn,

 

In our weekly research/development/idea/training sessions @ work, I decided to raise this particular custom function as a little training exercise to see how people might accomplish it.

 

The end result actually turned out to be a custom function far far simpler than my first attempt, far more efficient and faster, and that works :)

 

I have attached an example file containing the custom function. Hopefully this will fix your problem of having the last series of brackets not evaluate.

 

This function is so elegant that it only requires 1 parameter ( as opposed to my original six).

 

This is a good example of why, when building a custom function, to not always go with your first idea of how to accomplish it, like I did :)

Share this post


Link to post
Share on other sites
gyre46

I would really like to try this solution, but the system will not allow me to download the file.

What am I doing wrong?

 

Could you post the CF in text format?

Share this post


Link to post
Share on other sites
Weetbicks

In order to download any sample/demo files from the forums, you need to subscribe as a member. I highly recommend you do so because it is money well spent for what you get back from this forum.

Share this post


Link to post
Share on other sites
daanvdn

Dear Weetbix,

 

Forgive the lateness of my reply: in the past I received an e-mail when someone replied to an item I posted, but for some obscure reason this setting is disabled...

 

Thanks again for the time and effort you put in my questions! and thanks for the CF!

 

Superb!

 

Kind regards,

 

Daan Van den Nest

Ghent

Belgium

Share this post


Link to post
Share on other sites
This thread is quite old. Please start a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.




×
×
  • Create New...

Important Information

Terms of Use