get_explodedkey ( )

Function stats

Average user rating
266
281
9999
Support
FileMaker 7.0 +
Date posted
05 November 2010
Version
1
Recursive function
No

Author Info
 Rewolfer

5 functions

Average Rating None

author_avatar



 

Function overview

Prototype

get_explodedkey  ( string;   delimiter;   nr;   direction )


Parameters

string  the string to be parsed for keys


delimiter  delimiter as "|"


nr  occurrence of key


direction  search the string from "left" or "right"


Description

Tags:  Parse   Key   Exploded  

If you need to extract certain values from a pre-defined "value-chain" as in many exports this function might help you to get the key. You can parse the string from left or right and specify occurrence.

Examples

Sample input

get_explodedkey ("all$you$need$is$love"; "$", 1; "right")


Sample output

love

 

Function code

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

function gets a key from a string with ONLY ONE delimiter
if string does not BEGIN or END with delimiter, the delimiter will be added as first and last char to ensure string always starts with a possible key

structure:
get_explodedkey (string; delimiter; nr; direction)

arguments:
string = any string
delimiter = delimiters like "," or "|" etc; if no delimiter is given or delimiter cannot be found in string function returns the entire string
nr = occurrence of key that is wanted
direction = counting occurrence from "left" or "right"; default is "left"

example:
get_explodedkey ("|alle|menschen|sind|gut|"; "|"; 2; "left") --> "menschen"
get_explodedkey ("alle$menschen$sind$gut"; "$", 2; "right") --> "sind"

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

Let (
[
/* if string doesn't START or END with delimiter it will be appended */
string = Case (
Left (string; 1) ? delimiter; delimiter & string;
string
);
string = Case (
Right (string; 1) ? delimiter; string & delimiter;
string
);

/* calculating start of occurrence depending if we count from left or right */
delimiter_count = PatternCount ( string; delimiter);
delimiter_occurrence = Case (
((direction = "") or (direction = "left")); nr;
delimiter_count - nr
);

key_start = Position ( string; delimiter; 1; delimiter_occurrence) + 1;
key_end = Position ( string; delimiter ; 1; (delimiter_occurrence + 1))

];

Middle (
string;
key_start;
(key_end - key_start)
)

)

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

    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/281

    Prototype: get_explodedkey( string; delimiter; nr; direction )
    Function Author: Rewolfer (http://www.fmfunctions.com/mid/266)
    Last updated: 05 November 2010
    Version: 1

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