I know that to initialize a one dimensional array, we can do:
$array = @()
Is it possible to initialize a two dimensional array in the same way?
I just know how to do this via: $array = new-object 'object[,]' 50,2 but I
would need to know how many elements there were first. With $array = @(), I
can just add to it via: $array += $var, which is convienent.
Thanks in advance,
IMHO, this has little to do with array initialization, and everything to do
with the difference between a one dimensional array and a multidimensional
array.
If you had a two-dimensional array of indeterminate bounds, where,
precisely, would you want a single element added to the end of it to go?
That said, some languages allow the final bound to be boundless, so perhaps
powershell has some equivalent to:
$array = new-object 'object[,]' 50,*
/Al
There is a great article on arrays in PowerShell here
http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in-powershell.aspx
Specifically, the comma is the array operator in PowerShell.
55 > $a = (1,2),(3,4)
56 > $a[0][1]
2
57 > $a[1][0]
3
To initialize a 2d array, you can do something like this.
60 > $a = ,,1
61 > $a[0][1]
62 > $a[0][0]
1
63 >
Hope that helps,
If you find yourself leaning towards a 2dim array.. you are probably doing
something wrong.
Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
A> On Oct 19, 10:01 pm, "Al Dunbar" <AlanD...@hotmail.com.nospaam>
A> wrote:
A>
>> "Frank" <Fr...@discussions.microsoft.com> wrote in message
>>
>> news:11965E72-7BC4-4BD4...@microsoft.com...
>>
>>> Hi,
>>>
>>> I know that to initialize a one dimensional array, we can do:
>>>
>>> $array = @()
>>>
>>> Is it possible to initialize a two dimensional array in the same
>>> way?
>>>
>>> I just know how to do this via: $array = new-object 'object[,]' 50,2
>>> but I
>>> would need to know how many elements there were first. With $array
>>> = @(),
>>> I
>>> can just add to it via: $array += $var, which is convienent.
>> IMHO, this has little to do with array initialization, and everything
>> to do with the difference between a one dimensional array and a
>> multidimensional array.
>>
>> If you had a two-dimensional array of indeterminate bounds, where,
>> precisely, would you want a single element added to the end of it to
>> go?
>>
>> That said, some languages allow the final bound to be boundless, so
>> perhaps powershell has some equivalent to:
>>
>> $array = new-object 'object[,]' 50,*
>>
>> /Al
>>
A> There is a great article on arrays in PowerShell here
A>
A> http://blogs.msdn.com/powershell/archive/2007/01/23/array-literals-in
A> -powershell.aspx
A>
A> Specifically, the comma is the array operator in PowerShell.
A>
A> 55 > $a = (1,2),(3,4)
A> 56 > $a[0][1]
A> 2
A> 57 > $a[1][0]
A> 3
A> To initialize a 2d array, you can do something like this.
A>
A> 60 > $a = ,,1
A> 61 > $a[0][1]
A> 62 > $a[0][0]
A> 1
A> 63 >
A> Hope that helps,
A>
A> Andy
A> http://www.get-powershell.com
I have a situation where I download data from a SQL table and I need to keep
the rows intact with the columns. There would be 10 rows downloaded, then I
would prompt to users for modifications of these columns within the rows. If
there was change, I would modify the data in the SQL table accordingly. What
is the best technique for this?
Thanks in advance,
Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
f> Thanks for the replies,
f>
f> I have a situation where I download data from a SQL table and I need
f> to keep the rows intact with the columns. There would be 10 rows
f> downloaded, then I would prompt to users for modifications of these
f> columns within the rows. If there was change, I would modify the
f> data in the SQL table accordingly. What is the best technique for
f> this?
f>
f> Thanks in advance,
f>
f> "Brandon Shell [MVP]" wrote:
f>
This example gets the first 3 rows from the Person.Contact table in the AdventureWorks
database
and updates the first name of the first two rows. Then it updates the source
table with updated rows only.
$svr = ".\sqlexpress"
$db = "adventureworks"
$connString = "server=$svr;Integrated Security=SSPI;database=$db"
$queryString = "SELECT TOP 3 ContactID,FirstName,LastName,EmailAddress FROM
Person.Contact"
$sda = new-object System.Data.SqlClient.SqlDataAdapter $queryString,$connString
$ds = new-object System.Data.DataSet
# Automatically generates single-table commands that are used to reconcile
# changes made to a DataSet with the associated SQL Server database.
$scb = new-object System.Data.SqlClient.SqlCommandBuilder $sda
# Adds or refreshes rows in the with the results of $queryString
$sda.fill($ds)
# make a change
$ds.tables[0].rows[0]["FirstName"]="Gustavo1"
$ds.tables[0].rows[1]["FirstName"]="Catherine1"
# update all dataset rows
#$sda.update($ds)
# update only if changes were made to the DataSet
# the returned value (int32) is the number of rows
# successfully updated from the DataSet (should be 2 in this case)
if($ds.hasChanges()){ $sda.update($ds.getChanges("Modified")) }
---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar
F> Thanks for the replies,
F>
F> I have a situation where I download data from a SQL table and I need
F> to keep the rows intact with the columns. There would be 10 rows
F> downloaded, then I would prompt to users for modifications of these
F> columns within the rows. If there was change, I would modify the
F> data in the SQL table accordingly. What is the best technique for
F> this?
F>
F> Thanks in advance,
F>
F> "Brandon Shell [MVP]" wrote:
F>