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
LVL 1
mikeydkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
you are moving from a 1 dimensional array to a 2 dimensional array.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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?
0
CLR BenjaminCommented:
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
0
mikeydkAuthor Commented:
David Johnson, CD, MVP> Yes ;)
0
oBdACommented:
So you want to keep a relation between the input you fed to "calc" and the results for each input value?
Don't use a two-dimensional array for that; the VBScript days are over, and rightfully so.
The following will create custom objects to return, with one property 'Value' with the original value passed to the function, and a property 'Result' with the calculated result.
$example = ("AAA", "BBB", "CCC")

Function Calc { 
	Param($Value)
	$ReturnObject = '' | Select-Object -Property 'Value', 'Result'
	$ReturnObject.Value = $Value
	$ReturnObject.Result = "test1"
	Return $ReturnObject
}

$example | % { calc -Value $_}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.