Jump to content
tomtill

returns value if present in string

Recommended Posts

tomtill

What I need is a function that will test whether any member of a value list is in a text string, if true, it should return the value.

 

"the brown cow needs milk"

 

compare to value list "milk toast|jaguar|brown cow|red cow"

 

should return "brown cow"

 

This differs from most requirements in that the value list may contain multiple words, while the text string is a sentence.

 

One option is to explode the text string into a list of each word, followed by pairs of words, followed by phrases of 3., similar to the ExplodeKey custom function for individual words.

 

However, I am having trouble wrapping my head around nested recursive functions, and I don't have time to fully grok it right now.

 

Can someone point me to a custom function that does this?

 

For example, this

 

ExplodeKey (string)

 

Case ( string = "" ; "" ;

 

ExplodeKey ( LeftWords ( string ; Wordcount (string) - 1 ) ) & string & "¶"

)

)

 

will get me a list with the first word followed by each additional word

 

But next I need to lop off the first word, then do it again, then lop of the next word...

 

How do I do this?

Share this post


Link to post
Share on other sites
Weetbicks

Your recursive custom function should loop through your list of values, not the text string, for each value of the list it could use the Patterncount function on the text string and if found, append it to a new list, ie:

 

 

functionName ( textString ; valueList )

 

 

if (

isEmpty ( valueList ) ; "" ;

if (

PatternCount ( textString ; GetValue ( valueList ; 1 ) ) > 0 ; GetValue ( valueList ; 1 ) ; "" ) &

functionName ( textString ; rightValues ( valueList ; valueCount ( valuelist ) - 1 ) ) ) ;

 

)

)

 

 

So it checks if the list of values is empty (the end condition) and if it is it returns blank. However if there are still values in the list, it checks whether the first value appears in the string. If it does, it appends it to a result list (otherwise it doesnt). Finally it runs the recursion on the rest of the list.

Share this post


Link to post
Share on other sites
tomtill

Thanks, Weetbix, that is good insight.

 

But, the function doesn't work as written. If I remove the last ";))" the syntax is satisfied, but the function evaluates to ?.

 

 

 

functionName ( textString ; valueList )

 

 

if (

isEmpty ( valueList ) ; "" ;

if (

PatternCount ( textString ; GetValue ( valueList ; 1 ) ) > 0 ; GetValue ( valueList ; 1 ) ; "" ) &

functionName ( textString ; rightValues ( valueList ; valueCount ( valuelist ) - 1 ) ) ) ;

 

)

)

 

 

Share this post


Link to post
Share on other sites
tomtill

my mistake

 

after removing the syntax errors it works great!

 

I was using the CFExplorer function tester, and it was giving me the ?

 

When I put it into FM's Data Viewer tool and tested it, it worked fine.

 

Many thanks.

 

Now I will just continue to stare at it until I figure out HOW it works.

 

The thing that drives me crazy about this and other recursive functions, is that I don't see HOW the variable changes.

 

For example in this case, the variable "valueList" is changing, but I don't see where it was defined to be any different. How did we "remove" the first value, then the second, etc to test each one?

Share this post


Link to post
Share on other sites
Weetbicks

the ValueList parameter is being modified when the function is recalled, it is in this step:

 

functionName ( textString ; rightValues ( valueList ; valueCount ( valuelist ) - 1 ) )

 

More specifically the rightvalues function. This takes from the list the right most values (basically all excluding the leftmost). It does this using the valuecount function. It finds out how many values are in the list, and subtracts 1, ie if the list was:

 

Blue

Green

Red

Pink

White

Orange

 

 

Then

ValueCount ( list ) = 6

ValueCount ( list ) - 1 = 5

 

Rightvalues ( List ; ValueCount ( list ) - 1 ) =

RightValues ( List ; 5 ) =

 

Green

Red

Pink

White

orange

 

 

does that make sense? Each time the recursion happens, the list that is passed to the new function call has 1 less item on the list, the item that is removed is the leftmost, why? Because the leftmost is the only item that is checked during each recursion.

Share this post


Link to post
Share on other sites
tomtill

I think I get it.

 

So in this example:

// SYNTAX: NumRange ( From ; To )

 

 

Let( pN = GetAsNumber(RightWords(From; 1));

 

If(pN

NumRange(From & “, “ & (pN + 1); To);

From)

)

 

//

 

The variable From is being changed to From & “, “ & (pN + 1) when it is called recursively by NumRange?

Share this post


Link to post
Share on other sites
Weetbicks

Correct, though that looks like a rather odd function, not quite sure what it is supposed to do.

Share this post


Link to post
Share on other sites
Weetbicks

I would post it, but I always find coming up for a decent name for such a function to be a real nightmare. You are welcome to post it on there if you wish.

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