How to implement indirection

Viewed 42

How does one indirectly refer to anything? In my case it's a column name. The "Field Access" section of the language doc left me with no clue.

For example

let
    OriginalStrings = "some string with {tkn1} and/or {tkn2}"
    ,ParamTable = [tkn1 = "this", tkn2 = "that"]
    ,Result = List.Accumulate(Record.FieldNames(ParamTable),OriginalStrings
                             ,(txt,current) => Text.Replace(txt,"{"&current&"}",ParamTable[current]))
in
    Result

This results in: Field 'current' of record not found. What I want is the value of 'current' as an identifier and not the literal.

The code above should result in "some string with this and/or that"

1 Answers

Similar to my answer here, Expression.Evaluate may work for what you're after.

Replace ParamTable[current] with

Expression.Evaluate("ParamTable["&current&"]", [ParamTable=ParamTable])

Check out the Expression.Evaluate() and non global environments section of this article for more clarity about the environment argument.

Related