1

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?

2
  • Perhaps you should rephrase your question. What type shall the vars have to have an empty spot? Can't grep the meaning of only print those values inside $apples that have a value greater than 2 characters in $oranges Commented Feb 26, 2017 at 15:17
  • Well the values inside both variables are in the same order, but some values inside the second variable are empty and contain just a space like " ".. So what i want is for example if the length of the third value inside $oranges is less than 2, it also ignores or skips the third value inside $apples.. Then i wanna write the output like first value of $apples and first value of $oranges, then second value of $apples and second value of $oranges, etc etc Commented Feb 26, 2017 at 15:29

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly, you saved me so much headache man, thanks a million

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.