R
R
Ras2019-07-30 19:10:03
PowerShell
Ras, 2019-07-30 19:10:03

Where does the extra console output come from when using .add on an arraylist?

Good day.
Why is the number of the array element additionally displayed in the console when the script is executed?
Script:

$buffer1 = New-Object System.Collections.ArrayList
              
for ($i=0; $i -le 10; $i++)
    {
    $temp="line $i"
    $buffer1.Add("`"$temp`",")   
    Write-Host $buffer1[$i]        
    }

Conclusion:
0
"line 0",
1
"line 1",
2
"line 2",
3
"line 3",
4
"line 4",
5
"line 5",
6
"line 6",
7
"line 7",
8
"line 8",
9
"line 9",
10
"line 10",

PS: I can’t formulate the question correctly in order to find it with a search.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2019-07-31
@Ras

The output of the array number is a feature of the ArrayList you are using.
Each time $buffer1.Add() is added, it prints out the row number in the array where it was added.
If you don't need this data, you can redirect it to $Null like this $buffer1.Add("`"$temp`",") > $Null
In this case line numbers will not be output to the console.
You can read more here https://learn-powershell.net/2014/09/13/quick-hits...

A
azarij, 2019-07-30
@azarij

it's hard to tell where it comes from, but if you do it like this, then there is no list number in the output:
$buffer1 = @()
for ($i=0; $i -le 10; $i++)
{
$temp="line $i"
$buffer1 += "`"$temp`","
Write-Host $buffer1[$i]
}
probably something specific to the Add method in the System.Collections.ArrayList object.

M
Mikhail Osher, 2019-07-30
@miraage

I don't know PowerShell, but one line raises a lot of suspicions.
Somehow the quotes are written asymmetrically.
$buffer1.Add("`"$temp`",")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question