Answer the question
In order to leave comments, you need to log in
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]
}
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",
Answer the question
In order to leave comments, you need to log in
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...
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.
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 questionAsk a Question
731 491 924 answers to any question