Home > other >  How do i pass an array into a function?
How do i pass an array into a function?

Time:04-22

I have the following code down below that will take an array and write out the correlating pixel colors to the powershell window. The array itself is to form an image. At this point I just have the one image as an example but intend to add more arrays to generate more images. When that time comes I want to be able to pass that array into the function to draw it out. As I have my code now I am getting an error saying

"Cannot index into a null array"

How do I properly pass an Array object into a function?

$Colors = @{
    1         =   'White'               
    2         =   'Black'         
    3         =   'DarkBlue'    
    4         =   'DarkGreen'     
    5         =   'DarkCyan'      
    6         =   'DarkRed'       
    7         =   'DarkMagenta'   
    8         =   'DarkYellow'    
    9         =   'Gray'          
    10        =   'DarkGray'      
    11        =   'Blue'          
    12        =   'Green'         
    13        =   'Cyan'          
    14        =   'Red'           
    15        =   'Magenta'       
    16        =   'Yellow'         
}

$omg  =   @(2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1), 
          @(2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2),
          @(2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,2,1,1,1,2,2,2),
          @(2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,2,1,1,1,2,2,2),
          @(2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,2,1,1,1,2,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2),
          @(2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1),
          @(2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1),
          @(2,2,1,1,1,1,2,2,2,1,1,1,1,1,1,2,2,2,1,1,1,1),
          @(2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
          @(2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
          @(2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2),
          @(2,2,2,2,1,1,1,1,1,1,2,2,2,2,1,1,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,1,1,2,2,2,2,1,1,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2),
          @(2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2),
          @(2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2),
          @(2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,2,2,2,2,2,2,2)


Function PS-Draw { 
[CmdletBinding()]

param ( 
[Parameter (Mandatory = $True, ValueFromPipeline = $True)]
[Alias("I")]
$Image

)

cls
       
foreach ($row in $Image)
{
  foreach ($position in $row) {
    Write-Host "  " -NoNewline -BackgroundColor $Colors[$position]; Start-Sleep -m 10
  }
  Write-Host ""
}
}

PS-Draw -Image $omg

CodePudding user response:

Just cast your Image parameter to [array] or [object[]] and remove ValueFromPipeline = $True:

function PS-Draw { 
    [CmdletBinding()]
    param ( 
        [Parameter (Mandatory = $True)]
        [Alias("I")]
        [array]$Image
    )

    cls
   
    foreach ($row in $Image) {
      foreach ($position in $row) {
        Write-Host "  " -NoNewline -BackgroundColor $Colors[$position]
        Start-Sleep -m 10
      }
      Write-Host ""
    }
}

Since the function no longer takes input via the pipeline, you need to call it using

PS-Draw -Image $omg

OR, if you do want to be able to send the array by piping it to the function, do:

function PS-Draw { 
    [CmdletBinding()]
    param ( 
        [Parameter (Mandatory = $True, ValueFromPipeline = $True)]
        [Alias("I")]
        [object[]]$Image
    )

    # if the data is sent through the pipeline, use $input to collect is as array
    if ($PSCmdlet.MyInvocation.ExpectingInput) { $Image = @($input) }

    cls
   
    foreach ($row in $Image) {
      foreach ($position in $row) {
        Write-Host "  " -NoNewline -BackgroundColor $Colors[$position]
        Start-Sleep -m 10
      }
      Write-Host ""
    }
}

Now you can also call the function using

$omg | PS-Draw

Result:

enter image description here

  • Related