![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||