pulling elements from one array and creating a new one.

I need some array assistance, I have a list and I need to pull elements from this list and produce some output. I have used powershell for years, but never consider myself as a programmer and there are some simple things a true coder might have a quick answer for. I am doing some research and playing with some things, but I wanted to post a question here in to see what a more experience coder might have to say…

I have a list of names and values produced in a piece of code…I want to take pieces of multiple elements and create a new output array.

NAME,value
Apple,red
Apple,fruit
Turtle,green
Turtle,tail
Tree,leafy
Tree,tall
Cloud,white
Cloud,fluffy

I want to extract the elements above and produce a list like so…

Name,value1,value2
Apple,red,fruit
Turtle,green,tail
Tree,leafy,tall
Cloud,white,fluffy

I can think of a few kludgy ways to do it but I could use an example. As I begin to code up my own creations I will add them, but I am almost certain an experienced coder may have a faster and more elegant example. Thanks!

I think I have a good start, but for some reason the $NewReport += $combine line is not giving me all the values, in my test echos, I am referencing the correct variable.

$a = "Apple","red"
$b = "Apple","fruit"
$c = "Turtle","green"
$d = "Turtle","tail"
$e = "Tree","leafy"
$f = "Tree", "tall"
$g = "Cloud","white"
$h = "Cloud","fluffy"


$List = $a,$b,$c,$d,$e,$f,$g,$h
$report = @()
$combine = @()
$NewReport = @()

foreach ($item in $List){

$row = "" | select Name,Value1
$row.Name = $item[0]
$row.Value1 = $item[1]
$report += $row

}

echo $report

echo "`nFirst Report Complete`n`n"



for($i=0;$i -le $report.length-1;$i++){
echo "loop $i"

echo $report[$i].Name
echo $report[$i].Value1
echo $report[$($i+1)].Value1

	if ($report[$i].Name -eq $report[$($i+1)].Name){
	echo "true"
	$combine = "" | select Name,ValueA,ValueB
	$combine.Name = $report[$i].Name
	$combine.ValueA = $report[$i].Value1
	$combine.ValueB = $report[$($i+1)].Value1
	$NewReport += $combine
	echo $combine
	}

}
echo "`n Start NewReport `n"
echo $NewReport

Here is my output to this script…notice the output after “start NewReport” only shows the .Name portion

Powershell> .\Array000.ps1

Name   Value1
----   ------
Apple  red   
Apple  fruit 
Turtle green 
Turtle tail  
Tree   leafy 
Tree   tall  
Cloud  white 
Cloud  fluffy

First Report Complete


loop 0
Apple
red
fruit
true
Apple        
loop 1
Apple
fruit
green
loop 2
Turtle
green
tail
true
Turtle       
loop 3
Turtle
tail
leafy
loop 4
Tree
leafy
tall
true
Tree         
loop 5
Tree
tall
white
loop 6
Cloud
white
fluffy
true
Cloud        
loop 7
Cloud
fluffy

 Start NewReport 

Apple        
Turtle       
Tree         
Cloud        

I have managed to get it to work, appearently it appears on the $NewReport I had to use |ft to get multiple elements to appear, so they were there all along…

** this list assumes that the name pairs are next to each other…for my purposes of what I am working on this is ok since my output will always be this way…

So, is there a more experienced way to do this? My solution required me to reference the element by index number and do a little math to get the value of the next match…

$a = "Apple","red"
$b = "Apple","fruit"
$c = "Turtle","green"
$d = "Turtle","tail"
$e = "Tree","leafy"
$f = "Tree", "tall"
$g = "Cloud","white"
$h = "Cloud","fluffy"


$List = $a,$b,$c,$d,$e,$f,$g,$h
$report = @()
$combine = @()
$NewReport = @()

foreach ($item in $List){

$row = "" | select Name,Value1
$row.Name = $item[0]
$row.Value1 = $item[1]
$report += $row

}

echo $report

echo "`nFirst Report Complete"



for($i=0;$i -le $report.length-1;$i++){

	if ($report[$i].Name -eq $report[$($i+1)].Name){
	$combine = "" | select Name,ValueA,ValueB
	$combine.Name = $report[$i].Name
	$combine.ValueA = $report[$i].Value1
	$combine.ValueB = $report[$($i+1)].Value1
	$NewReport += $combine
	}

}
$NewReport | ft

echo "`nNewReport Complete"

here is the output

Powershell> .\Array001.ps1

Name   Value1
----   ------
Apple  red   
Apple  fruit 
Turtle green 
Turtle tail  
Tree   leafy 
Tree   tall  
Cloud  white 
Cloud  fluffy

First Report Complete



Name   ValueA ValueB
----   ------ ------
Apple  red    fruit 
Turtle green  tail  
Tree   leafy  tall  
Cloud  white  fluffy



NewReport Complete
# String
$content = '
NAME,value
Apple,red
Apple,fruit
Turtle,green
Turtle,tail
Tree,leafy
Tree,tall
Cloud,white
Cloud,fluffy' 

# Convert string then group by name
$group = $content.Trim() | ConvertFrom-Csv | Group-Object -Property Name

foreach ($g in $group){
    [PSCustomObject]@{
        Name = $g.Name
        Values = $g.Group.value}
}

# Results
# Name                                           Values                                       
# ----                                           ------                                       
# Apple                                          {red, fruit}