Code - Custom Function

cfGetEveryNthValue ( pList ; pStart ; pIncrement ; pInputSeparator ; pOutputSeparator )

BrianDunning.com, Brian Dunning http://www.briandunning.com/filemaker-custom-functions/list.php


cfGetEveryNthValue ( pList ; pStart ; pIncrement ; pInputSeparator ; pOutputSeparator )
Rate this function: Average rating: 5.0 (1 vote) Discuss this Custom FunctionKevin Frank, Kevin Frank & Associates
http://www.kevinfrank.com
Extract every Nth item from a list; allow starting value and list separators to be specified
==================== SAMPLE INPUT AND OUTPUT ====================
INPUT
cfGetEveryNthValue ( "A~B~C~D~E~F" ; 2 ; 3 ; "~" ; "$" )
OUTPUT
B$E
==================== DESCRIPTION ====================
Recently I needed to reduce a list to "every other" item in that list, i.e., the 1st item, the 3rd item, the 5th item, etc.
It occurred to me that in the future I might need, say, every third item rather than every other item, and that I might want to begin with something other than the first item on the list. It also occurred to me that I might want to specify a list separator other than a hard return, for either the input, the output, or both.
That's how this CF came to be.
==================== CUSTOM FUNCTION ====================

/*

Purpose: Extract every Nth item from a list; allow starting value and list separators to be specified



Author: Kevin Frank & Associates (www.kevinfrank.com)



Usage: cfGetEveryNthValue ( pList ; pStart ; pIncrement ; pInputSeparator ; pOutputSeparator )



Example:

cfGetEveryNthValue ( "A~B~C~D~E~F" ; 2 ; 3 ; "~" ; "$" ) = "B$E"



Note: if you leave pInputSeparator and/or pOutputSeparator blank, ¶ will be used, e.g.,

cfGetEveryNthValue ( "A¶B¶C¶D¶E¶F" ; 1 ; 2 ; "" ; "" ) = "A¶C¶E"

*/



Let ( [

theInputSeparator = If ( IsEmpty ( pInputSeparator ) ; ¶ ; pInputSeparator ) ;

theOutputSeparator = If ( IsEmpty ( pOutputSeparator ) ; ¶ ; pOutputSeparator ) ;

theList = If ( theInputSeparator <> ¶ ; Substitute ( pList ; theInputSeparator ; ¶ ) ; pList )

] ;



GetValue ( theList ; pStart ) &



If (

ValueCount ( theList ) >= ( pStart + pIncrement ) ;

theOutputSeparator & cfGetEveryNthValue ( pList ; pStart + pIncrement ; pIncrement ; pInputSeparator ; pOutputSeparator )

)



) // end let


Referring URL: 'http://www.briandunning.com/cf/1118'

Attached File:    

Screenshot:    


Keywords: ''

ClipManager: