![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | initialize array Hi all, How can I initialize array before use him, before insert values to cells ? Thanks, Didi |
My System Specs![]() |
| | #2 (permalink) |
| | Re: initialize array If I understand you correctly then this should work. $array = @() "Did" <didi10000@walla.co.il> wrote in message news:1187620498.691958.85170@19g2000hsx.googlegroups.com... > Hi all, > How can I initialize array before use him, before insert values to > cells ? > Thanks, > Didi > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: initialize array Did wrote: > Hi all, > How can I initialize array before use him, before insert values to > cells ? > Thanks, > Didi > See my first line below: PSH> $a=@() PSH> $a.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array That's one way... Marco -- ---------------- PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #4 (permalink) |
| | Re: initialize array On Aug 20, 4:43 pm, "Brandon Shell" <tshell.m...@gmail.com> wrote: > If I understand you correctly then this should work. > $array = @() > > "Did" <didi10...@walla.co.il> wrote in message > > news:1187620498.691958.85170@19g2000hsx.googlegroups.com... > > > Hi all, > > How can I initialize array before use him, before insert values to > > cells ? > > Thanks, > > Didi Hi, I want to insert the value of $i to each cell in the array, I got the error: "Array assignment failed because index '0' was out of range" If I want trying to understand the message, while I want to insert the value of $i to cell 0, the shell doesn't found the index 0, maybe doesn't know I using array ! $arr=@() for ( $i = 0 ; $i -lt 3; $i++ ) { $arr[$i] = $i } write-host $arr This script with no error because probably I initialized the array: $arr= "dd","dd","dd" for ( $i = 0 ; $i -lt 3; $i++ ) { $arr[$i] = $i } write-host $arr Any help ? Thanks... |
My System Specs![]() |
| | #5 (permalink) |
| | Re: initialize array Try $arr=@() for ( $i = 0 ; $i -lt 3; $i++ ) { $arr[$i] += $i } write-host $arr This will add the value as well as extent the array. If you want to predefine the array you can do something like $array = @(1..100) # This will make an array of 100 but it will have values in each element. "Did" <didi10000@walla.co.il> wrote in message news:1187625216.058486.26150@r29g2000hsg.googlegroups.com... > On Aug 20, 4:43 pm, "Brandon Shell" <tshell.m...@gmail.com> wrote: >> If I understand you correctly then this should work. >> $array = @() >> >> "Did" <didi10...@walla.co.il> wrote in message >> >> news:1187620498.691958.85170@19g2000hsx.googlegroups.com... >> >> > Hi all, >> > How can I initialize array before use him, before insert values to >> > cells ? >> > Thanks, >> > Didi > > Hi, > I want to insert the value of $i to each cell in the array, I got the > error: > "Array assignment failed because index '0' was out of range" > > If I want trying to understand the message, while I want to insert the > value of $i to cell 0, the shell doesn't found the index 0, maybe > doesn't know I using array ! > > $arr=@() > > for ( $i = 0 ; $i -lt 3; $i++ ) > { > $arr[$i] = $i > } > write-host $arr > > > This script with no error because probably I initialized the array: > > $arr= "dd","dd","dd" > > for ( $i = 0 ; $i -lt 3; $i++ ) > { > $arr[$i] = $i > } > > write-host $arr > > Any help ? > Thanks... > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: initialize array This is for dynamic array. if you know the type and length of array you should able to declare array beforehand new-object int[] 3 "Brandon Shell" <tshell.mask@gmail.com> wrote in message news:%23RjqtP04HHA.5852@TK2MSFTNGP02.phx.gbl... > Try > > $arr=@() > > for ( $i = 0 ; $i -lt 3; $i++ ) > { > $arr[$i] += $i > } > write-host $arr > > This will add the value as well as extent the array. If you want to > predefine the array you can do something like > $array = @(1..100) # This will make an array of 100 but it will have > values in each element. > > "Did" <didi10000@walla.co.il> wrote in message > news:1187625216.058486.26150@r29g2000hsg.googlegroups.com... >> On Aug 20, 4:43 pm, "Brandon Shell" <tshell.m...@gmail.com> wrote: >>> If I understand you correctly then this should work. >>> $array = @() >>> >>> "Did" <didi10...@walla.co.il> wrote in message >>> >>> news:1187620498.691958.85170@19g2000hsx.googlegroups.com... >>> >>> > Hi all, >>> > How can I initialize array before use him, before insert values to >>> > cells ? >>> > Thanks, >>> > Didi >> >> Hi, >> I want to insert the value of $i to each cell in the array, I got the >> error: >> "Array assignment failed because index '0' was out of range" >> >> If I want trying to understand the message, while I want to insert the >> value of $i to cell 0, the shell doesn't found the index 0, maybe >> doesn't know I using array ! >> >> $arr=@() >> >> for ( $i = 0 ; $i -lt 3; $i++ ) >> { >> $arr[$i] = $i >> } >> write-host $arr >> >> >> This script with no error because probably I initialized the array: >> >> $arr= "dd","dd","dd" >> >> for ( $i = 0 ; $i -lt 3; $i++ ) >> { >> $arr[$i] = $i >> } >> >> write-host $arr >> >> Any help ? >> Thanks... >> > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: initialize array He is correct, but be careful if you use int[] you can only put integers in the array. Integers do int[] Strings do string[] Bytes do byte[] "x3shell" <xshell@gmail.com> wrote in message news:%23PHhGd04HHA.4436@TK2MSFTNGP03.phx.gbl... > This is for dynamic array. if you know the type and length of array you > should able to declare array beforehand > > new-object int[] 3 > > "Brandon Shell" <tshell.mask@gmail.com> wrote in message > news:%23RjqtP04HHA.5852@TK2MSFTNGP02.phx.gbl... >> Try >> >> $arr=@() >> >> for ( $i = 0 ; $i -lt 3; $i++ ) >> { >> $arr[$i] += $i >> } >> write-host $arr >> >> This will add the value as well as extent the array. If you want to >> predefine the array you can do something like >> $array = @(1..100) # This will make an array of 100 but it will have >> values in each element. >> >> "Did" <didi10000@walla.co.il> wrote in message >> news:1187625216.058486.26150@r29g2000hsg.googlegroups.com... >>> On Aug 20, 4:43 pm, "Brandon Shell" <tshell.m...@gmail.com> wrote: >>>> If I understand you correctly then this should work. >>>> $array = @() >>>> >>>> "Did" <didi10...@walla.co.il> wrote in message >>>> >>>> news:1187620498.691958.85170@19g2000hsx.googlegroups.com... >>>> >>>> > Hi all, >>>> > How can I initialize array before use him, before insert values to >>>> > cells ? >>>> > Thanks, >>>> > Didi >>> >>> Hi, >>> I want to insert the value of $i to each cell in the array, I got the >>> error: >>> "Array assignment failed because index '0' was out of range" >>> >>> If I want trying to understand the message, while I want to insert the >>> value of $i to cell 0, the shell doesn't found the index 0, maybe >>> doesn't know I using array ! >>> >>> $arr=@() >>> >>> for ( $i = 0 ; $i -lt 3; $i++ ) >>> { >>> $arr[$i] = $i >>> } >>> write-host $arr >>> >>> >>> This script with no error because probably I initialized the array: >>> >>> $arr= "dd","dd","dd" >>> >>> for ( $i = 0 ; $i -lt 3; $i++ ) >>> { >>> $arr[$i] = $i >>> } >>> >>> write-host $arr >>> >>> Any help ? >>> Thanks... >>> >> > |
My System Specs![]() |
| | #8 (permalink) |
| | Re: initialize array On Aug 20, 6:53 pm, "Brandon Shell" <tshell.m...@gmail.com> wrote: > He is correct, but be careful if you use int[] you can only put integers in > the array. > > Integers do int[] > Strings do string[] > Bytes do byte[] > > "x3shell" <xsh...@gmail.com> wrote in message > > news:%23PHhGd04HHA.4436@TK2MSFTNGP03.phx.gbl... > > > This is for dynamic array. if you know the type and length of array you > > should able to declare array beforehand > > > new-object int[] 3 > > > "Brandon Shell" <tshell.m...@gmail.com> wrote in message > >news:%23RjqtP04HHA.5852@TK2MSFTNGP02.phx.gbl... > >> Try > > >> $arr=@() > > >> for ( $i = 0 ; $i -lt 3; $i++ ) > >> { > >> $arr[$i] += $i > >> } > >> write-host $arr > > >> This will add the value as well as extent the array. If you want to > >> predefine the array you can do something like > >> $array = @(1..100) # This will make an array of 100 but it will have > >> values in each element. > > >> "Did" <didi10...@walla.co.il> wrote in message > >>news:1187625216.058486.26150@r29g2000hsg.googlegroups.com... > >>> On Aug 20, 4:43 pm, "Brandon Shell" <tshell.m...@gmail.com> wrote: > >>>> If I understand you correctly then this should work. > >>>> $array = @() > > >>>> "Did" <didi10...@walla.co.il> wrote in message > > >>>>news:1187620498.691958.85170@19g2000hsx.googlegroups.com... > > >>>> > Hi all, > >>>> > How can I initialize array before use him, before insert values to > >>>> > cells ? > >>>> > Thanks, > >>>> > Didi > > >>> Hi, > >>> I want to insert the value of $i to each cell in the array, I got the > >>> error: > >>> "Array assignment failed because index '0' was out of range" > > >>> If I want trying to understand the message, while I want to insert the > >>> value of $i to cell 0, the shell doesn't found the index 0, maybe > >>> doesn't know I using array ! > > >>> $arr=@() > > >>> for ( $i = 0 ; $i -lt 3; $i++ ) > >>> { > >>> $arr[$i] = $i > >>> } > >>> write-host $arr > > >>> This script with no error because probably I initialized the array: > > >>> $arr= "dd","dd","dd" > > >>> for ( $i = 0 ; $i -lt 3; $i++ ) > >>> { > >>> $arr[$i] = $i > >>> } > > >>> write-host $arr > > >>> Any help ? > >>> Thanks... Well done ! Thanks two of u ! |
My System Specs![]() |
| | #9 (permalink) |
| | Re: initialize array "Brandon Shell" wrote: > He is correct, but be careful if you use int[] you can only put integers in > the array. > > Integers do int[] > Strings do string[] > Bytes do byte[] and: Objects do object[] :-) -- greetings dreeschkind |
My System Specs![]() |
| | #10 (permalink) |
| | Re: initialize array The correct way to dynamically create an array is as follows (PowerShell will expand to the size required). You use the index notation as in $arr[$i] only when the individual items already exist in order to access / change them. HTH! PS > $arr=@() PS > for ( $i = 0 ; $i -lt 3; $i++ ) { $arr += $i } PS > $arr.length 3 PS > $arr.gettype().fullname System.Object[] PS > $arr 0 1 2 PS > for ( $i = 0 ; $i -lt 3; $i++ ) { $arr[$i] } 0 1 2 PS > $j = 10; for ( $i = 0 ; $i -lt $arr.length; $i++ ) { $arr[$i] = $j*$i } PS > $arr 0 10 20 -- ............... All new, all fresh .............. http://www.leedesmond.com/weblog/ "Did" wrote: > On Aug 20, 4:43 pm, "Brandon Shell" <tshell.m...@gmail.com> wrote: > > If I understand you correctly then this should work. > > $array = @() > > > > "Did" <didi10...@walla.co.il> wrote in message > > > > news:1187620498.691958.85170@19g2000hsx.googlegroups.com... > > > > > Hi all, > > > How can I initialize array before use him, before insert values to > > > cells ? > > > Thanks, > > > Didi > > Hi, > I want to insert the value of $i to each cell in the array, I got the > error: > "Array assignment failed because index '0' was out of range" > > If I want trying to understand the message, while I want to insert the > value of $i to cell 0, the shell doesn't found the index 0, maybe > doesn't know I using array ! > > $arr=@() > > for ( $i = 0 ; $i -lt 3; $i++ ) > { > $arr[$i] = $i > } > write-host $arr > > > This script with no error because probably I initialized the array: > > $arr= "dd","dd","dd" > > for ( $i = 0 ; $i -lt 3; $i++ ) > { > $arr[$i] = $i > } > > write-host $arr > > Any help ? > Thanks... > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Fast copy method of sub array (=array range) possible? | VB Script | |||
| Re: How to initialize array variable to have fixed size | PowerShell | |||
| Initialize Junk Mail Filtering and MSOE.DLL could not initialize | Vista mail | |||
| how to assign values to array and how to create array via variable | PowerShell | |||
| Initialize array without values | PowerShell | |||