Please cancel
Please cancel
  • Edited by ritz5 12 hours 8 minutes ago
September 3rd, 2015 2:27pm

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

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 2:37pm

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]
	}
}



September 3rd, 2015 2:44pm

jrv - I'm pretty sure that's actually a red herring.
Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 2:45pm

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.
September 3rd, 2015 2:51pm

Why not just use ForEach-Object? Then you don't need to know.
Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 2:52pm

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

September 3rd, 2015 2:53pm

Because it is better to use a for loop to increment each time, and a two dimensional array is like a multi dimensional array.
Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 2:54pm

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.

September 3rd, 2015 2:58pm

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.

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 3:01pm

Please cancel

Okay then.

September 3rd, 2015 3:07pm

Mike - I think you were right.

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 3:08pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics