logical.switch ( )

Function stats

Average user rating
4.0000
29
145
9999
Support
FileMaker 7.0 +
Date posted
16 December 2008
Last updated
02 February 2009
Version
Recursive function
No

Author Info
 Genx

18 functions

Average Rating 4.1

author_avatar



 

Function overview

Prototype

logical.switch  ( value;   tests;   results )


Parameters

value  The value / field to test


tests  A pilcrow (¶) delimited set of tests


results  A corresponding (¶) delimited set of results + an optional default value


Description

Tags:  Switch   PHP   Logical   Emulation  

Designed to mimic the switch statement available in various programming languages to the extent possible (primary difference: it will not continue evaluating tests after it finds a solution). The function attempts to find the value provided in tests and retrieves the corresponding result value. If no such value exists, it will attempt to retrieve an "excess" value from the results list.

Got the idea when I was writing a reply to: http://fmforums.com/forum/showtopic.php?fid/233/tid/193938

The idea of this function is to save you from writing tedious case statements e.g. the following could be replaced with the example provided in the examples section below (Age = 9):

Case(
    Age = 6 ; "Just Right" ;
    Age = 7 ; "A little old!!";
    Age = 8 ; "Far too old!!";
    "Really...?"
)

Outputs: Really...?

Examples

Sample input

logical.switch( "Teen" ; "Teen¶Adult¶Senior" ; "$25¶$50¶$75¶n/a" )


Sample output

$25

 

Function code

Let( itemPos = Position( ¶ & tests & ¶ ; ¶ & value & ¶ ; 0 ; 1 );    
    If( not itemPos ; MiddleValues( results ; ValueCount(tests)+1; 1 ) ; MiddleValues( results ; PatternCount ( Left( tests ; itemPos - 1 ) ; ¶ ) + 1 ; 1 ) )
)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/145

    Prototype: logical.switch( value; tests; results )
    Function Author: Genx (http://www.fmfunctions.com/mid/29)
    Last updated: 02 February 2009
    Version: 1.5

*/
// ===================================

 

Comments

Fabrice
16 December 2008



Hi,
if I can allow myself, I think the example (with a series of numbers) gives the impression that this is just a Choose() with a default parameter, where it is much more interesting than that.
Fabrice
  General comment
Genx
17 December 2008



Any idea's on a more useful example? ;)
  General comment

 

 

 

 

 

Top Tags

Text Parsing  (33)
List  (32)
Date  (28)
XML  (27)
Format  (23)
Sql  (22)
Dev  (20)
Debug  (17)
Interface  (15)
Layout  (15)
Text  (14)
Variables  (13)
Filter  (12)
Layout Objects  (11)
Design  (10)
Array  (8)