ArrayList of ArrayLists

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
azeiler
Posts: 12
Last visit: Thu Jan 20, 2022 4:44 pm

ArrayList of ArrayLists

Post by azeiler »

I've got an ArrayList of ArrayLists of Strings that I need to process with an advanced function. I am modeling that function off of what I learned in "Learn Powershell Toolmaking in a Month of Lunches."

In order to make the functions flexible, they tell you in that book to set it up so that a parameter can be piped in, but also taken as an array. So basically, you make the advanced function to accept ValuesFromPipeline for a particular parameter, but then you also make that parameter type an array and add a ForEach-Object in your Process.

What I am running into is that if I configure my function this way, it no longer works to pipe the values in. Because then it treats them one at a time, and the ForEach-Object breaks that ArrayList into its subsequent strings for processing. Is there an easy way to prevent this?

See code example below. When I pass $OutsideArray to Get-Data as a normal parameter (Get-Data $OutsideArray), it works. When I remove the ForEach-Object in the function, it works. But when I pipe $OutsideArray in as is, it no longer works.
  1. function Get-Data()
  2. {
  3.     [CmdletBinding()]
  4.     param (
  5.         [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
  6.         [System.Collections.ArrayList]$ModuleBlocks
  7.     )
  8.    
  9.     BEGIN
  10.     {
  11.     }
  12.     PROCESS
  13.     {
  14.         $ModuleBlocks | %{
  15.            
  16.             $test = $_
  17.  
  18.         }
  19.        
  20.     }
  21.     END { }
  22.    
  23. }
  24.  
  25. $OutsideArray = New-Object System.Collections.ArrayList
  26.  
  27. For ($i = 0; $i -lt 10; $i++)
  28. {
  29.    
  30.     $InsideArray = New-Object System.Collections.ArrayList
  31.    
  32.     For ($j = 0; $j -le 10; $j++)
  33.     {
  34.         $InsideArray.Add("$i This is a test $j") | Out-Null
  35.     }
  36.    
  37.     $OutsideArray.Add($InsideArray) | Out-Null
  38.    
  39. }
  40.  
  41. $OutsideArray | Get-Data
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ArrayList of ArrayLists

Post by jvierra »

The enumerator will enumerate the array in a pipeline. That is expected. How you design this depends on the intended use. I suggest gaining more experience with PowerShell and Advanced Functions before trying to do this. When yu have a real-world example then you will be able to ask a question that can be answered.

Run the following to see what is happening:

Code: Select all

function Get-Data() {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
        [System.Collections.ArrayList]$ModuleBlocks
    )
    begin {
        Write-Host 'Begin called'
    }
    process {
        Write-Host 'Process called' -Fore green
        $ModuleBlocks | 
            ForEach-Object{
                Write-Host "`t`tForeach loop" -Fore Yellow
                $test = $_
            }
    }
    end {
        Write-Host 'Begin called'
    }
}

$OutsideArray = New-Object System.Collections.ArrayList
For ($i = 0; $i -lt 10; $i++) {  
    $InsideArray = New-Object System.Collections.ArrayList
    For ($j = 0; $j -le 10; $j++) {
        $InsideArray.Add("$i This is a test $j") | Out-Null
    }
    $OutsideArray.Add($InsideArray) | Out-Null
}

$OutsideArray | Get-Data 
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ArrayList of ArrayLists

Post by jvierra »

This may make it more obvious.

Code: Select all


$OutsideArray = New-Object System.Collections.ArrayList
For ($i = 0; $i -lt 10; $i++) {  
    $InsideArray = New-Object System.Collections.ArrayList
    For ($j = 0; $j -le 10; $j++) {
        $InsideArray.Add("$i This is a test Outer: $i Inner: $j") | Out-Null
    }
    $OutsideArray.Add($InsideArray) | Out-Null
}

$OutsideArray | Get-Data
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked