Vivek Kumar Singh
Vivek Kumar Singh

Reputation: 3350

Extract value of one array from another array

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.

Upvotes: 1

Views: 461

Answers (1)

4c74356b41
4c74356b41

Reputation: 72171

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

Upvotes: 1

Related Questions