Author Posts

December 22, 2017 at 10:13 am

Hello,

I'm probably on the wrong track.
I have an array (values to process) over which I iterate in a foreach loop. If the current value of that array matches several criteria I want to move this value to an other array (processed values).
Is it possible to move a value from one array to an other? I help myself with copying the value to the new array and remove it afterwards in the old one. But is there a better solution?

December 22, 2017 at 2:53 pm

$Naughty = @(
    'Dasher'
    'Prancer'
    'Vixen'
    'Comet'
    'Cupid'
    'Dunder'
    'Blixem'
    'Rudolph'
)

$Nice = $Naughty | foreach {
    if ( # list of conditions here
        $PSItem -match 'ixe' -or
        $PSItem -match 'sh'
    ) { # matches copied into $Nice
        $PSItem
    } 
}
$Naughty = $Naughty | where  { $PSItem -notin $Nice } # $Nice items removed from $Naughty

"Naughty: $($Naughty -join ', ')"
"Nice:    $($Nice -join ', ')"