0

This answer here: Powershell break a long array into a array of array with length of N in one line?

almost perfectly answers my requirements. What I would like to do is group my elements so that the first group has one extra element in it and all others have the first element repeated in each group.

Like this:

$bigList = ("Name","One","Two","Three","Four","Five","six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen")

$counter = [pscustomobject] @{ Value = 0 }
$groupSize = 5

$groups = $bigList | Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) }

With my groups ending up looking like this ...

Name, One, Two, Three, Four, Five
Name, Six, Seven, Eight, Nine, Ten
Name, Eleven, Twelve, Thirteen
3
  • Sounds like a code smell... Commented Nov 16, 2017 at 5:45
  • Not helpful ... thanks for reading though. Commented Nov 16, 2017 at 5:57
  • I don't think there will be a simple way of adapting the group-object method. That method assigns a dynamic property to each element and groups accordingly. Each element will end up in one group and one group only. I suspect you'll need to programmatically break up $biglist, stripping off the first element. After that, group-object may be used to generate the individual groups, but further processing will be necessary to prefix each group with the first element from the original biglist. Commented Nov 16, 2017 at 5:59

1 Answer 1

1

How's this:

$bigList = ("Name","One","Two","Three","Four","Five","six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen")

$counter = [pscustomobject] @{ Value = 0 }
$groupSize = 5

$bigList[1..$biglist.count] |
    Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) } |
    foreach-object {
        $_.Group.Insert(0,$biglist[0])
        write-output $_
    }

Output:

Count Name                      Group                                                                                                                                                                                                      
----- ----                      -----                                                                                                                                                                                                      
    5 0                         {Name, One, Two, Three...}                                                                                                                                                                                 
    5 1                         {Name, six, Seven, Eight...}                                                                                                                                                                               
    3 2                         {Name, Eleven, Twelve, Thirteen}

This works by stripping out the first element ("Name") and sending the rest of the array through the group-object cmdlet. The resulting objects are modified in the foreach-object scriptblock by inserting the value from the first element of the big list into the group property. The modified object is re-written to the pipeline.

Assuming you want these in an list of arrays, you'd need to modify the code slightly:

$arrayList = $bigList[1..$biglist.count] |
    Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) } |
    ForEach-Object {
        $_.Group.Insert(0,$biglist[0])
        ,$_.group
    }

$arraylist | 
    % {
        write-host ( $_ -join "," )
    }

Here, we deliberately output only the group property of the groupinfo object that comes from group-object. In addition, we use the , operator to force powershell to output an individual array. If this is not done, we end up with a single array that is just like $biglist, but with extra instances of 'Name' every 5 elements. The final chunk of code outputs the contents of each element of $arrayList, concatenating with commas to demonstrate that we do indeed have an array of arrays:

Name,One,Two,Three,Four,Five
Name,six,Seven,Eight,Nine,Ten
Name,Eleven,Twelve,Thirteen
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Sooooo much! I'll check it out tomorrow. I'm sure that this well written Nugget will help me immensely!!!
Fingers crossed. Using group-object like this is quite clever. I've learned something myself today!
Absolutely spot on for what I asked ... Now I just hope that I can implement it in the way that I want to! Thanks again for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.