Link to home
Start Free TrialLog in
Avatar of mikeydk
mikeydkFlag for Denmark

asked on

Powershell - Add to array

Hey

$example = ("AAA", "BBB", "CCC")

function calc {
    param ()
   
    return "test1"
    }


$example | % { calc }

How do I add the value from calc to the array? (ext. the array - not just string concatenate)

AAA TEST1
BBB TEST1
CCC TEST1

Mike
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

you are moving from a 1 dimensional array to a 2 dimensional array.
You mean as another "column"? That would need an array of objects, e.g. of arrays. What do you want to do with the result?
Maybe a trick:

function calc {
    param ()
   
    return "test1"
    }

$example = ("AAA", "BBB", "CCC")
$count = $example.count
$count = $count -1
$array = 0..$count |  foreach-object{
$example[$_]| foreach-object {new-object psobject -property @{ $_ = calc $_}}
}

try

$array | select-object AAA
$array | fl
Avatar of mikeydk

ASKER

David Johnson, CD, MVP> Yes ;)
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial