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 - Stupid Array Tricks: Initializing an Array to a Certain Size

Reply
 
Old 09-08-2008   #1 (permalink)
tojo2000


 
 

Stupid Array Tricks: Initializing an Array to a Certain Size

Okay, so I'm trying to use the SqlDataReader.GetValues() method, and
it wants me to pass it an array. The problem is that it wants the
array to be at least as large as the data that I want to put it in,
otherwise the data gets truncated.

I whipped this up, but it doesn't seem like the most efficient way:

foreach ($i in 0..($reader.FieldCount - 1)) {
$row += $null
}

Is there a better way to gin up an empty array of a specific size (a
contradiction, I know, but you know what I mean)?

My System SpecsSystem Spec
Old 09-08-2008   #2 (permalink)
Shay Levy [MVP]


 
 

Re: Stupid Array Tricks: Initializing an Array to a Certain Size


PS > $arr = new-object object[] $reader.FieldCount



---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic

t> Okay, so I'm trying to use the SqlDataReader.GetValues() method, and
t> it wants me to pass it an array. The problem is that it wants the
t> array to be at least as large as the data that I want to put it in,
t> otherwise the data gets truncated.
t>
t> I whipped this up, but it doesn't seem like the most efficient way:
t>
t> foreach ($i in 0..($reader.FieldCount - 1)) {
t> $row += $null
t> }
t> Is there a better way to gin up an empty array of a specific size (a
t> contradiction, I know, but you know what I mean)?
t>


My System SpecsSystem Spec
Old 09-09-2008   #3 (permalink)
Hal Rottenberg


 
 

Re: Stupid Array Tricks: Initializing an Array to a Certain Size

tojo2000 wrote:
Quote:

> Is there a better way to gin up an empty array of a specific size (a
> contradiction, I know, but you know what I mean)?
This came up on #powershell some time ago and I blogged a really short solution.
Then Bruce Payette followed up with more info which was awesome, so click the
link to see that.

http://halr9000.com/article/430

method 1:

$array = ,0 * 20

method 2:

$array = @(0) * 20

--
Author, Tech Prosaic blog (http://halr9000.com)
Webmaster, Psi (http://psi-im.org)
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Fast copy method of sub array (=array range) possible? VB Script
Re: Dim? (array of fixed size) PowerShell
Re: Dim? (array of fixed size) PowerShell
RE: Dim? (array of fixed size) PowerShell
how to assign values to array and how to create array via variable 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