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 - initialize array

Reply
 
Old 08-20-2007   #1 (permalink)
Did


 
 

initialize array

Hi all,
How can I initialize array before use him, before insert values to
cells ?
Thanks,
Didi


My System SpecsSystem Spec
Old 08-20-2007   #2 (permalink)
Brandon Shell


 
 

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 SpecsSystem Spec
Old 08-20-2007   #3 (permalink)
Marco Shaw


 
 

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 SpecsSystem Spec
Old 08-20-2007   #4 (permalink)
Did


 
 

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 SpecsSystem Spec
Old 08-20-2007   #5 (permalink)
Brandon Shell


 
 

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 SpecsSystem Spec
Old 08-20-2007   #6 (permalink)
x3shell


 
 

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 SpecsSystem Spec
Old 08-20-2007   #7 (permalink)
Brandon Shell


 
 

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 SpecsSystem Spec
Old 08-20-2007   #8 (permalink)
Did


 
 

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 SpecsSystem Spec
Old 08-20-2007   #9 (permalink)
dreeschkind


 
 

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 SpecsSystem Spec
Old 08-20-2007   #10 (permalink)
Desmond Lee


 
 

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 SpecsSystem Spec
Reply

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


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