Forum Discussion

JackTradeOne's avatar
JackTradeOne
Copper Contributor
Mar 19, 2021
Solved

Default values for LAMBDA paramters - Part II

Based on the responses to this question, I decided to try the answer out on a function I picked up somewhere, which splits a string based on a specified delimiter.

Using a LAMBDA function without default parameters, and feeding it the two parameters (the string - several words with space between them, specified in A1 - and the delimiter - space, for this example):

 

=LAMBDA(string,delimiter, 
TRANSPOSE(FILTERXML("<t><s>" & SUBSTITUTE(string, delimiter, "</s><s>") & "</s></t>",  "//s") ) 
)(A1," ")

 

everything works as expected.

Setting space as the default parameter and NOT including it in the parameters submitted, also works as expected:

 

=LAMBDA(string,delimiter,
TRANSPOSE(
    FILTERXML(
        "<t><s>" &
        SUBSTITUTE(
            string,
            IF(delimiter,delimiter," "),
            "</s><s>"
        ) & "</s></t>",
        "//s"
    )
)
)(A1,)

 

However, feeding both parameters to the same LAMBDA (whether the delimiter is space or anything else):

 

(A1," ")

 

 

raises a #VALUE! error.

 

What to do?

  • JackTradeOne 

    That's since IF("text", ... returns #VALUE!. In previous sample I assumed using of numbers.

    Here you may change on IF(delimiter<>0,delimiter," ")

    As a comment, I'd do not recommend to use FILTERXML as general function for splitting the text, only if we assume if the text has no inside some special symbols which are not recognized by this function. For example, splitting of  a,b&c,d returns an error since instead of & you shall use &amp; . Similar for some other characters.

10 Replies

  • SergeiBaklan's avatar
    SergeiBaklan
    Diamond Contributor

    JackTradeOne 

    That's since IF("text", ... returns #VALUE!. In previous sample I assumed using of numbers.

    Here you may change on IF(delimiter<>0,delimiter," ")

    As a comment, I'd do not recommend to use FILTERXML as general function for splitting the text, only if we assume if the text has no inside some special symbols which are not recognized by this function. For example, splitting of  a,b&c,d returns an error since instead of & you shall use &amp; . Similar for some other characters.

    • JackTradeOne's avatar
      JackTradeOne
      Copper Contributor

      Thanks! I didn't realize IF("TEXT",... raises an error. I'll use your suggestion or IF(delimiter<>"",delimiter," ").

       

      As to your comment about the problem with using FILTERXML to split strings by delimiter - I definitely agree, but I have to say this is the only solution I know of to do that for strings with more than two delimiters (those can be split with RIGHT, LEFT, MID). It's really strange that something as basic as this, which in (for example) python and javascript can be accomplished with something as simple as string.split(" ") requires people to resort to gymnastics such as converting the string to xml, running an xpath expression on it and then transposing the output...

      • tboulden's avatar
        tboulden
        Iron Contributor

        JackTradeOne 

         

        Here's my latest iteration implementing a text split LAMBDA; you can provide a delimiter to split on, or if you leave that parameter blank, it splits on each char:

        =LAMBDA(str,del,
            LET(
                dsd,del&str&del,
                posArray,
                    IF(
                        ISBLANK(del),SEQUENCE(LEN(str)+1),
                        FIND(
                            CHAR(1),
                            SUBSTITUTE(
                                dsd,
                                del,
                                CHAR(1),
                                SEQUENCE((LEN(dsd)-LEN(SUBSTITUTE(dsd,del,"")))/LEN(del))
                            )
                        )
                    )-1,
                startPosArray,INDEX(posArray,SEQUENCE(ROWS(posArray)-1)),
                splitPosArray,INDEX(posArray,SEQUENCE(ROWS(posArray)-1,1,2,1)),
                charArray,MID(str,startPosArray+1,splitPosArray-startPosArray-LEN(del)),
                valArray,IFERROR(VALUE(charArray),charArray),
                valArray
            )
        )

         

Resources