Array.PowerShell 어레이 찾기
배열을 사용하는 방법.파워셸에서 메서드를 찾으시겠습니까?
예:
$a = 1,2,3,4,5
[Array]::Find($a, { args[0] -eq 3 })
기브즈
Cannot find an overload for "Find" and the argument count: "2".
At line:3 char:1
+ [Array]::Find($a, { $args[0] -eq 3 })
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
어레이 클래스는 다음과 같이 제가 예상하는 방식을 가지고 있습니다.
PS> [Array] | Get-Member -Static
TypeName: System.Array
Name MemberType Definition
---- ---------- ----------
Find Method static T Find[T](T[] array, System.Predicate[T] match)
배열을 T[] 유형과 일치하도록 다른 위치로 캐스팅해야 합니까?찾기 기능을 수행할 수 있는 다른 방법이 있다는 것은 알고 있지만, 왜 이것이 작동하지 않는지 궁금합니다.
어레이를 사용할 필요가 없습니다.찾기, 정규where
조항이 잘 작동할 것입니다.
$a = @(1,2,3,4,5)
$a | where { $_ -eq 3 }
또는 이것(@mjolinor에서 제안한 바와 같이):
$a -eq 3
또는 이것(반환)$true
또는$false
):
$a -contains 3
Where
절은 다음과 같은 기본 유형뿐만 아니라 모든 유형의 개체를 지원합니다.
$a | where { $_.SomeProperty -eq 3 }
당신은 캐스팅이 필요합니다.ScriptBlock
로서Predicate[T]
다음 예를 생각해 보십시오.
[Array]::Find(@(1,2,3), [Predicate[int]]{ $args[0] -eq 1 })
# Result: 1
오류가 발생한 이유는 PowerShell을 전달하는 경우 일치하는 메서드 오버로드가 없었기 때문입니다.ScriptBlock
에서 언급한 바와 같이Get-Member
출력, 없음Find()
메서드 오버로드를 허용합니다.ScriptBlock
두 번째 매개 변수로 사용됩니다.
[Array]::Find(@(1,2,3), { $args[0] -eq 1 })
"찾기"에 대한 오버로드를 찾을 수 없으며 인수 개수는 "2"입니다. 줄:1 문자:17 + [Array]::찾기(@(1,2,3), {$_-eq 1} + ~~~ + 카테고리정보: 지정되지 않음: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldFindBest
또 다른 옵션은 를 사용하는 것입니다. 이는 다음을 제공합니다.Contains
방법:
PS C:\> [Collections.ArrayList]$a = 'a', 'b', 'c'
PS C:\> $a.Contains('b')
True
PS C:\> $a.Contains('d')
False
또는 @Neolisk가 댓글에 언급했듯이 PowerShell의-contains
연산자:
PS C:\> $a = 'a', 'b', 'c'
PS C:\> $a -contains 'b'
True
PS C:\> $a -contains 'd'
False
Find() 정적 메서드뿐만 아니라 FindIndex()에 대해서도 Trevor Sullivan의 답변이 맞습니다.
서버에 ipv4와 ipv6가 모두 활성화된 NIC 카드가 여러 개 있고 ipv4 IP/netmask 쌍을 확인하려는 경우 다음과 같은 방법이 좋습니다.
$NetworkAdapters = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IPEnabled = True' | Select-Object -Property Description, IPAddress, IPSubnet, DefaultIPGateway, DNSServerSearchOrder, DNSDomain
$NetworkAdapters | % {
"Adapter {0} :" -f $_.Description
# array'ing to avoid failure against single homed netcards
$idx = [System.Array]::FindIndex(@($_.IPAddress), [Predicate[string]]{ $args[0] -match "\d+.\d+.\d+.\d+" })
" IP {0} has netmask {1}" -f @($_.IPAddress[$idx]), @($_.IPSubnet)[$idx]
}
제 요점은 2012 WinPE에서 매력적으로 작동하고 Win7 wks 프로덕션에서 실패한다는 것입니다.생각나는 사람?
이는 시스템에서 최대 600만 개의 항목에 걸쳐 실행되었습니다.두 가지 방법을 모두 사용한 배열
$s=get-date
$([array]::FindALL($OPTArray,[Predicate[string]]{ $args[0] -match '^004400702(_\d{5})?' })).count
$(New-TimeSpan -Start $s -End $(get-date)).TotalSeconds
20 items
33.2223219 seconds
$s=get-date
$($OPTArray | where { $_ -match '^004400702(_\d{5})?'}).count
$(New-TimeSpan -Start $s -End $(get-date)).TotalSeconds
20 items
102.1832173 seconds
언급URL : https://stackoverflow.com/questions/21209946/array-find-on-powershell-array
'sourcecode' 카테고리의 다른 글
iphone/ipad(실제 장치, 시뮬레이터 없음)의 문서 폴더에 액세스하는 방법이 있습니까? (0) | 2023.08.16 |
---|---|
antMatcher와 mvcMatcher의 차이 (0) | 2023.08.16 |
고정 영역을 제외한 전체 화면을 어둡게 합니까? (0) | 2023.08.16 |
웹킷을 C/C++/Win32 응용 프로그램에 내장하는 방법은 무엇입니까? (0) | 2023.08.16 |
Android 에뮬레이터가 시작되지 않고 "잘못된 명령줄 매개 변수"가 표시됩니다. (0) | 2023.08.16 |