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 Tutorial - output multiple arrays to file

Reply
 
Old 01-02-2009   #1 (permalink)
Justin Rich
Guest


 
 

output multiple arrays to file

trying to find the best way to take 3 arrays and output them to a file so
that each line would be $a1[$i], $a2[$i], $a3[$i]

im sure this is easy, but i keep tripping over it.

Thanks
Justin



My System SpecsSystem Spec
Old 01-02-2009   #2 (permalink)
Kryten
Guest


 
 

Re: output multiple arrays to file

Hi Justin,

This assumes that your arrays all contain the same number of elements.

$a = 1..10 ; $b = 11..20 ; $c = 21..30
foreach ( $i in (0..$a.count) ) {
Add-Content -path "arraytest.txt" `
-value "$($a[$i])`r`n$($b[$i])`r`n$($c[$i])"
}

Just one way though, bet there are dozens of other|better ways.

Cheers,
Stuart
My System SpecsSystem Spec
Old 01-03-2009   #3 (permalink)
Kryten
Guest


 
 

Re: output multiple arrays to file

Ahem. Or even:-

$a = 1..10 ; $b = 11..20 ; $c = 21..30
foreach ( $i in (0..($a.count -1)) ) {
Add-Content -path "arraytest.txt" `
-value "$($a[$i])`r`n$($b[$i])`r`n$($c[$i])"

}

Cheers,
Stuart

My System SpecsSystem Spec
Old 01-03-2009   #4 (permalink)
Kryten
Guest


 
 

Re: output multiple arrays to file

It occurred to me that there must be a simple way to combine two
arrays.
But I couldn't think of it, so wrote this very simple function that
might be of use to
someone, somewhere, someday.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function combine-arrays ( $array1,$array2 ) {
if ($array1.count -ne $array2.count) { "Arrays are different sizes" ;
exit }
$newarray = @()
$c = $array1.count
for ( $i = 0; $i -le $c; $i++ ) {
$newarray += $array1[$i]
$newarray += $array2[$i]
}
return $newarray
}


$a = 1..10
$b = 11..20

combine-arrays $a $b
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hope it helps,

Stuart
My System SpecsSystem Spec
Old 01-06-2009   #5 (permalink)
Justin Rich
Guest


 
 

Re: output multiple arrays to file

Thanks! This was what I was looking for.


"Kryten" <Kryten68@xxxxxx> wrote in message
news:27853c17-c198-4a9a-a885-3a28a2570705@xxxxxx
Quote:

> Ahem. Or even:-
>
> $a = 1..10 ; $b = 11..20 ; $c = 21..30
> foreach ( $i in (0..($a.count -1)) ) {
> Add-Content -path "arraytest.txt" `
> -value "$($a[$i])`r`n$($b[$i])`r`n$($c[$i])"
>
> }
>
> Cheers,
> Stuart
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can Vista Ultimate output to multiple audio devices? Vista hardware & devices
Print to File/Output file name issue Vista print fax & scan
Problem with multiple audio/video sound output Sound & Audio
Output to multiple devices similtaneously? Sound & Audio
output format with multiple commands 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