2

Hello and good evening to yall

I was looking to see if I can get some assistance in suppressing the count when adding to ArrayList. For some reason I dont understand even after my google searches, I just can't suppress the count.

[System.Collections.ArrayList]$AStuff = @()
[System.Collections.ArrayList]$CStuff = @()
[System.Collections.ArrayList]$Rest = @()

switch -Wildcard (gci C:\users\abrah\OneDrive\Desktop){
    "D*" {$AStuff.Add($_)}
    "C*" {$CStuff.Add($_)}
    Default {$Rest.Add($_)}
    }

for($i=0; $i -lt ($AStuff.Count + $CStuff.Count + $Rest.Count); $i++){
    [pscustomobject]@{
        "Letter A" = $AStuff[$i]
        "Letter C" = $CStuff[$i]
        "Rest " = $Rest[$i]
    }
}

the output is:


0
1
0
1
0
1
2
3
2
3
4

Letter A Letter C                  Rest                            
-------- --------                  -----                           
dir1     CFR-2008-title38-vol1.pdf Test                            
dir2     cs.csv                    v1                              
         CSV                       error.png                       
         csvv.csv                  Screenshot 2021-01-09 195943.png
                                   v2.pdf                          
                                                 

Can someone educate me on what is going on in regards to why it outputs the count?

Please dont feel the need to answer if its something really simple, a brief explanation will do and I should be able to fix it if thats the case.

  • On a side note, is there a more efficient way to add to an object like its shown above?
Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • Please pardon my ignorance if im not explaining it correctly – Abraham Zinala Feb 08 '21 at 04:02
  • 1
    In short: any output - be it from a command or a .NET method call - that is neither captured nor redirected (sent through the pipeline or to a file) is _implicitly output_. To simply _discard_ such output, it's best to use `$null = ...`; see [this answer](https://stackoverflow.com/a/55665963/45375) to the linked duplicate. Here, the [`.Add()` method](https://learn.microsoft.com/en-US/dotnet/api/System.Collections.ArrayList.Add) produces implicit output (the index of the newly added element). – mklement0 Feb 08 '21 at 04:16
  • @Mkelement0, thank you for that; gave it an extra upvote. Where do you recommend I learn more about this type of stuff? Any books? Websites? (Ive read up on both "Learn Ps in a month of lunches", and "PS for SysAdmins") I just feel like theres a lot more I can learn. Should I dive into .net and C# for a better understanding of PS? – Abraham Zinala Feb 08 '21 at 04:32
  • 1
    Thanks, Abraham. A while back I compiled a short list of learning resources in [this answer](https://stackoverflow.com/a/48491292/45375). It certainly helps to know about .NET data types and APIs, but I wouldn't start there; I'd say C# is only needed for understanding code samples in the .NET documentation. Otherwise: PowerShell is great for hands-on experimentation, and you'll also find plenty of good information in the answers her on SO. – mklement0 Feb 08 '21 at 04:38

1 Answers1

4

This is just because Add method returns an int :

$a =@()
$a.Add

OverloadDefinitions                 
-------------------                 
int IList.Add(System.Object value) 

So in you case you can avoid it using | out-null.

$AStuff.Add($_) | out-null
JPBlanc
  • 70,406
  • 17
  • 130
  • 175