Advertisement

01.28.2008 at 02:52PM PST, ID: 23117816
[x]
Attachment Details

PowerShell - Array of arrays

Asked by PC_User321 in MSH/Monad PowerShell

Tags: PowerShell

I have data in a pipeline from which I want to create an array of arrays like, for example:
  (a, XXa), (b, XXb), (c, XXc)
I don't want to use a pre-declared multi-dimensional array because it is messy and I don't know in advance how big it will need to be.

This code does not work - is gives a length of 6
$arrayOfArrays = ("a","b","c") | %{($_, "XX$_")}
# Check if it worked:
"arrayOfArrays[2] = " + $arrayOfArrays[2]
"Length = " + $arrayOfArrays.length


This code works but it is messy:
$tmp = @(1)
$arrayOfArrays = @()

("a","b","c") | %{ $tmp[0] = ($_, "XX$_"); $arrayOfArrays += $tmp;}
# Check if it worked:
"arrayOfArrays[2] = " + $arrayOfArrays[2]
"Length = " + $arrayOfArrays.length

Is there a better way?
Start Free Trial
 
Loading Advertisement...
 
[+][-]01.28.2008 at 04:26PM PST, ID: 20764336

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.29.2008 at 07:28AM PST, ID: 20768553

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.30.2008 at 12:41AM PST, ID: 20775514

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.30.2008 at 05:16AM PST, ID: 20776687

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.30.2008 at 09:21AM PST, ID: 20778919

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MSH/Monad PowerShell
Tags: PowerShell
Sign Up Now!
Solution Provided By: BSonPosh
Participating Experts: 1
Solution Grade: A
 
 
[+][-]01.30.2008 at 11:30PM PST, ID: 20784645

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628