Agterbosch
Agterbosch

Reputation: 15

Multiple objects inside variables, output them one by one with powershell

I got a question, I've been searching high and low for this but cant find an answer (also my coding skills are zero to none).. In powershell I have 2 variables, for example say $apples and $oranges. They both contain a list of values that are not yet split and $oranges sometimes contains an empty value, like

$apples = red green blue yellow purple
$oranges = car bike   chair shirt

They are in the correct order

Now what I'm trying to achieve is split the values inside the variables, but only print those values inside $apples that have a value greater than 2 characters in $oranges

So that it skips "blue" because in $oranges that spot is empty and the output in the text file would become something like this:

Apples red
Oranges car


Apples green
Oranges bike


Apples yellow
Oranges chair


Apples purple
Oranges shirt

Is there any mastermind out there that could help me?

Upvotes: 1

Views: 2269

Answers (1)

user6811411
user6811411

Reputation:

I edited your question to make any sense, you can't set a string containing spaces to a var without quoting. I still don't know what the meaning of that sentence is.

This script:

$apples = ("red green blue yellow purple").split(' ')
$oranges = ("car bike  chair shirt").split(' ')
for ($i=0;$i -le $apples.Length; $i++){
  if ($oranges[$i]){
    "Apples {0}" -f $apples[$i]
    "Oranges {0}" -f $oranges[$i]
    ""
  }
}

Returns this output:

>  .\SO_42469662.ps1
Apples red
Oranges car

Apples green
Oranges bike

Apples yellow
Oranges chair

Apples purple
Oranges shirt

Upvotes: 2

Related Questions