1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PowerShell】配列内の文字列を検索する個人的メモ

Posted at

#配列内の空文字列を除外したい。
テキストのフィルタリングをできるだけ軽くしたい。
パイプラインでWhere-ObjectやSelect-Objectを使用すると非常に遅くなる。
なので、できるだけパイプラインを使用しない形で配列内の空文字列を除外したい。
#現状で思いつく最適解。

# 配列の要素が空文字であるインデックス番号を配列として取得する
[Array]$text = @("aaa","aaa","","aaa","aaa","","aaa","","d","")
[Array]$text2 = @("aaa","aaa","","","aaa","","aaa","","d","")
[Array]$ArrayList = @( $text, $text2 )

# 空文字列のインデックス番号を取得する。
[int[]]$NullStringIndexList = @()
for($i=0; $i -lt $ArrayList.Length; $i++){
    [int]$index = -1
    while($TRUE){
        $index = [Array]::IndexOf($ArrayList[$i], "", $index + 1)
        if($index -ne "-1"){
            $NullStringIndexList += $index
        } else {
            break
        }
    }
}
$NullStringIndexList = $NullStringIndexList | Sort-Object -Unique

#空文字列でないインデックス番号を取得。$IndexListへ格納する。 
[int[]]$IndexList = 0..($ArrayList[0].Length -1)
$NullStringIndexList.ForEach({ $IndexList = $IndexList -ne $_ })
1
1
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?