- Edited by ritz5 12 hours 8 minutes ago
Hi all, i was hoping someone could help me with the above question.
I've researched online about two dimensional arrays and accessing elements in an array. And how you can display an array by using a for loop. This hasn't helped.
I've found an example below online, but this is different to what i have. I'll explain below the code;
$array=@("Row1-Col1","Row1-Col2","Row1-Col3"),("Row2-Col1","Row2-Col2","Row2-Col3") for ($col=0;$col -le $array.length;$col++) { for ($row=0;$row -le $array[$col].length;$row++) { echo $array[$col][$row] } }
I have a $array1 this stores two sets of information; example get-childitem Name and Last Date Modified.
I want to access the elements in the $array1 and output this in a 2 dimensional format.
Contributors please can you add a short note how this works.
thanks in advance
If your description is correct, $array1 is actually an object.
To access the individual items, you'd use $array1[0].Name or $array1[0].LastWriteTime.
What do you mean by '2 dimensional fo
In PowerShell we normally use objects to store related data.
Also your array is not an array;
$array=@( @("Row1-Col1","Row1-Col2","Row1-Col3"), @("Row2-Col1","Row2-Col2","Row2-Col3") ) for($c=0;$c -lt $array.length;$c++){ for ($r=0;$r -lt $array[$c].length;$r++){ $array[$c][$r] } }
Ignore the example I found; but if I do get-child item it will return 10 files or another day it may return 20 files. I need to understand how to put this in a two dimensional way and to add a row for each new file. I've seen the operators ++ and += But more of what I need help with is accessing the elements in the array each time I run the script the results will be different. So the for loop will stop when it is equal to the array object count.
I would start by first learning PowerShell. Once you understand the basics you will either understand how to do this or you will be able to ask a question that makes sense.
Start here and post back when you have the basics: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396
There is absolutely no need to use arrays in this situation in PowerShell. Please go study the basics of PowerShell and post back when you understand how it is intended to work.
Modern languages have not built based on arrays for more than 2 decades. We use objects. All things in PowerShell and Windows are either object or values.
Because it is better to use a for loop to increment each time, and a two dimensional array is like a multi dimensional array.
But you don't have an array, you have an object.
Please cancel
Okay then.
Mike - I think you were right.