1

I have a few arrays as shown below.

$a1 = "apple"
$a2 = "ball"
$a3 = "cat"
$a4 = "dog"
$a5 = "egg"

Now, I am trying to store the values of the above arrays in a separate array as shown below.

$array = @()
for($i = 1; $i -le 5; $i++) {$array += "$" + "a$i"}

Now $array give me the following

PS C:\> $array
$a1
$a2
$a3
$a4
$a5
PS C:\>

What I want now is if I can extract the value of array $a1which is apple, instead of the array's $array value $a1. I would like to extract the value "apple" of $a1 from $array. Somebody who has done this before, can you please shed some light on this. Thanks.

2
  • $a1 = "apple" is not an array Commented Apr 25, 2017 at 8:01
  • Yes 4c74356b41. You are right. My bad. The base type for $a1 is indeed System.Object. Thanks for pointing that out. The below answer works for me. :) Commented Apr 25, 2017 at 9:06

1 Answer 1

1

You could do this:

for($i = 1; $i -le 5; $i++) {$array += Get-Variable ("a" + $i)}

this would get variables a1,a2... and store them into array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.