views:

449

answers:

1

I would like to do something like this. Index into an array of functions and apply the appropriate function for the desired loop index.

for ($i = 0; $i -lt 9; $i++)
{
    $Fields[$i] = $Fields[$i] | $($FunctionTable[$i])
}
#F1..F9 are defined functions or rather filter functions

$FunctionTable =  {F1}, 
                {F2}, 
                {F3},
                {F4},
                {F5},
                {F6},
                {F7},
                {F8},
                {F9}
+11  A: 

Here's an example of how to do this using the call (&) operator.

# define 3 functions
function a { "a" }
function b { "b" }
function c { "c" }

# create array of 3 functioninfo objects
$list = @(
  (gi function:a),
  (gi function:b),
  (gi function:c)
)

0, 1, 2 | foreach {
  # call functions at index 0, 1 and 2
  & $list[$_]
}

-Oisin

p.s. this means your pipeline should bve amended to something like:

$Fields[$i] = $Fields[$i] | & $FunctionTable[$i]
x0n
Very cool. Thanks a lot!
tellingmachine