2answers

Wrap lines of list

If you have a return separated list how could you wrap each line like this?

Cat

Elephant

Woodpecker

Dog

Mongoose

To this

<wrap>Cat</wrap>

<wrap>Elephant</wrap>

<wrap>Woodpecker</wrap>

<wrap>Dog</wrap>

<wrap>Mongoose</wrap>

Chris Johnston

Answers

Let ( [

~list = "Cat¶Elephant¶Woodpecker¶Dog¶Mongoose" ;

~start = "<wrap>" ;

~end = "</wrap>" ;

~middle = ~end & ¶ & ~start

] ;

~start

& Substitute ( ~list ; ¶ ; ~middle )

& ~end

)

Jeremy Bante

Whoa, that's a slick solution!

That definitely beats using a custom function with tail recursion!

Thanks, Kevin. It could still be worthwhile to wrap this up in a custom function so that it's encapsulated, so the developer doesn't have to deal with the cognitive load of thinking about how it works every time they look at the calculation.

Also, I presume you're refering to your own solution when you mention "using a custom function with tail recursion." The functions you present are indeed recursive, but they aren't tail recursive. To be tail recursive, they would have to be structured so that only the last recursion returned any of the result:

Recursive function:

If ( ~finished ; ~partialResult ; /* Else */ ~partialResult & RecursiveFunction )

Tail-recursive function:

If ( ~finished ; ~fullResult ; /* Else */ TailRecursiveFunction )

In FileMaker, there are a couple different tricks we can use to make any recursive function tail-recursive, even if the algorithm being implemented isn't naturally amenable to tail recursion. My personal favorite is using the Let() function to set variables that persist from one function call to the next. Check out this function for an example.

Here are a few custom functions that I wrote years ago to work with list.


The first one builds a list with the current found set of records.

BuildList ( Field ; Found )

Case (

Found > 1 ;

GetNthRecord ( Field ; Found ) & "¶" & BuildList ( Field ; Found - 1 ) ;

GetNthRecord ( Field ; 1 ) )


Example

Set Variable [ $List ; BuildList ( Jobs::Name ; Get ( FoundCount ) ) ]


The second builds a list with related records.

BuildListRelated ( Field ; NumberOfRecords )

Case (

NumberOfRecords > 1 ;

GetValue ( List ( Field ) ; NumberOfRecords ) & "¶" & BuildListRelated ( Field ; NumberOfRecords - 1 ) ;

GetValue ( List ( Field ) ; 1 ) )


Example

Set Variable [ $List ; ListBuildRelated ( Sales::__kP_Sales ; Count ( Sales::__kP_Sales ) ) ]


The third one wraps a list.

WrapList ( TheValues ; NumberOfValues )

Case (

NumberOfValues > 1 ;

"*" & GetValue ( TheValues ; NumberOfValues ) & "*" & "¶" & WrapList ( TheValues ; NumberOfValues - 1 ) ;

"*" & GetValue ( TheValues ; 1 ) & "*" )


Example

$List = "1¶2¶3¶4¶5"

Set Variable [ $ListWrapped ; WrapList ( $List ; ValueCount ( $List ) ) ]

Kevin Kurpe

Back to questions