Convert string to int array in powershell -
i'm trying convert string looks this
2,3,4,5,6,20..30
to array of integers. here code have:
[string]$a = "2,3,4,5,6,7" [array]$b = $a.split(",") [array]$c = foreach($number in $b) {([int]::parse($number))}
which works, not range of 20..30. how part working?
you can use invoke-expression
cmdlet interpret 10..30
bit, if [int]::parse()
method call fails.
here complete, working sample.
[string]$a = "2,3,4,5,6,7,10..30"; [array]$b = $a.split(","); [array]$c = foreach($number in $b) { try { [int]::parse($number) } catch { invoke-expression -command $number; } } $c;
Comments
Post a Comment