Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - how to assign values to array and how to create array via variable

Reply
 
Old 03-13-2007   #1 (permalink)
Frank


 
 

how to assign values to array and how to create array via variable

Hi,

I need to assign values to an array, but they may not be in order. ie,

$ar[0] = "test"
but I get the error:

Array assignment failed because index '0' was out of range.
At line:1 char:5
+ $ar[0 <<<< ] = "test"

I can add them in sequence, via: $ar += "test" but I cannot add via specific
element.

Also, is there a way to create an array via a variable name, ie:

$var1 = "testname"

$(`$var1`) = @()

The goal is to create an array $testname.

Thanks in advance,

Frank



My System SpecsSystem Spec
Old 03-13-2007   #2 (permalink)
Clint Bergman


 
 

Re: how to assign values to array and how to create array via variable

You can create an array like this:
$varArray = "Value1","Value2","Value3","Value4","Value5"

and append values as you noted:
$varArray += "Value6"

I don't think you can just randomly assign a value to an arbitrary element.
Changing an arbitrary element is easy though. So what I have done in the
past is something like this:

# Create the array with however many elements I think I might need:
$Arr = 1..20
# Now I can modify any of the elements (0-19, not 1-20)
$Arr[2] = "Value1"
$Arr[6] = "Value6"
# And append more elements in chunks if I need
$Arr += 1..10

for more info:
get-help about_array

~Clint

"Frank" <Frank@discussions.microsoft.com> wrote in message
news:104CD347-3D9A-4546-BC64-4A41E9702912@microsoft.com...
> Hi,
>
> I need to assign values to an array, but they may not be in order. ie,
>
> $ar[0] = "test"
> but I get the error:
>
> Array assignment failed because index '0' was out of range.
> At line:1 char:5
> + $ar[0 <<<< ] = "test"
>
> I can add them in sequence, via: $ar += "test" but I cannot add via
> specific
> element.
>
> Also, is there a way to create an array via a variable name, ie:
>
> $var1 = "testname"
>
> $(`$var1`) = @()
>
> The goal is to create an array $testname.
>
> Thanks in advance,
>
> Frank
>
>



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to create array without quotes? $array = (a,b,c) PowerShell
How do i retrieve only the values from array ? PowerShell
Read-Host values into an Array PowerShell
Getting all values out of an array of objects PowerShell
Initialize array without values PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46