locked
A simple counting script with color change; Why is my script doing this? RRS feed

  • Question

  • Please help, I can't find out why this is not working :(

    echo off
    For ($A=5; $A -le 252; $A++)
    	{For ($B=5; $B -le 252; $B++)
    		{For ($C=5; $C -le 252; $C++)	
    		{
    		($color1 = "DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White") | Get-Random
    		write-host -Foreground $color1 1.$A.$B.$C 
    		}
    		
    	}
    }

    The results I get are

    DarkYellow
    1.5.7.244
    Red
    1.5.7.245
    White
    1.5.7.246
    Magenta
    1.5.7.247
    Gray
    


    BlankMonkey


    • Edited by BlankMonkey Wednesday, July 22, 2015 11:25 PM simplier
    Wednesday, July 22, 2015 11:12 PM

Answers

  • The issue isn't in the Write-Host line. See this adjustment of your original code:

    For ($A=250; $A -le 252; $A++) {
        For ($B=250; $B -le 252; $B++) {
            For ($C=250; $C -le 252; $C++) {
    		    $color1 = ("DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White" | Get-Random)
    		    Write-Host -Foreground $color1 1.$A.$B.$C 
    		}
    	}
    }


    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 6:01 PM
  • Your code:

      ($color1 = "DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White") | Get-Random

    What is in $color1?


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 6:02 PM
  • In your code $color1 is an array of all the colors. If you only wanted one random color you need to remove the brackets like so: 

    $color1 = "DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White" | Get-Random

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:32 PM
    Thursday, July 23, 2015 6:03 PM
  • what did you expect?


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 2:24 AM
  • Try this and think about it:

    $colors='DarkBlue','DarkGreen','DarkCyan','DarkRed','DarkMagenta','DarkYellow','Gray','DarkGray','Blue','Green','Cyan','Red','Magenta','Yellow','White'
    $colors|%{write-host '$_`:' -NoNewline; Write-Host 'This is test text' -fore $_}


    \_(ツ)_/


    • Edited by jrv Thursday, July 23, 2015 2:32 AM
    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 2:31 AM
  • Now think about this one:

    $colors='DarkBlue','DarkGreen','DarkCyan','DarkRed','DarkMagenta','DarkYellow','Gray','DarkGray','Blue','Green','Cyan','Red','Magenta','Yellow','White'
    1..10|%{Write-Host 'This is a test' -fore ($colors|get-random)}
    


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 2:35 AM
  • You were close and just needed to do a little more experimentation.


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 5:26 PM
  • Actually it is more like Tai Chi.  Kung Fu is a hammer. Tai Chi is a paintbrush.  Kung Fu learns from the empty hand.


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 5:29 PM
  • I also simplified your code by taking the array assignment out of the loop.  Why recreate the same array each time.


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 6:03 PM

All replies

  • what did you expect?


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 2:24 AM
  • Try this and think about it:

    $colors='DarkBlue','DarkGreen','DarkCyan','DarkRed','DarkMagenta','DarkYellow','Gray','DarkGray','Blue','Green','Cyan','Red','Magenta','Yellow','White'
    $colors|%{write-host '$_`:' -NoNewline; Write-Host 'This is test text' -fore $_}


    \_(ツ)_/


    • Edited by jrv Thursday, July 23, 2015 2:32 AM
    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 2:31 AM
  • Now think about this one:

    $colors='DarkBlue','DarkGreen','DarkCyan','DarkRed','DarkMagenta','DarkYellow','Gray','DarkGray','Blue','Green','Cyan','Red','Magenta','Yellow','White'
    1..10|%{Write-Host 'This is a test' -fore ($colors|get-random)}
    


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 2:35 AM
  • Thank you I will :)

    BlankMonkey

    Thursday, July 23, 2015 4:38 PM
  • Thank you!

    Your powershell kungfu is strong ;)

    They both appear to work.  I will see if I can do some reading before I ask any more questions.......


    BlankMonkey

    Thursday, July 23, 2015 5:25 PM
  • You were close and just needed to do a little more experimentation.


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 5:26 PM
  • Actually it is more like Tai Chi.  Kung Fu is a hammer. Tai Chi is a paintbrush.  Kung Fu learns from the empty hand.


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 5:29 PM
  • So, I have my question;

    How is this

                Write-Host 'This is a test' -fore ($colors|get-random)

    Different from this;

                write-host -Foreground ($color1) 1.$A.$B.$C

    All I see is that for mine, I have picked the color first, and passed it over, while yours passes all the colors, and then picks one.


    BlankMonkey


    • Edited by BlankMonkey Thursday, July 23, 2015 5:48 PM correction
    Thursday, July 23, 2015 5:47 PM
  • The issue isn't in the Write-Host line. See this adjustment of your original code:

    For ($A=250; $A -le 252; $A++) {
        For ($B=250; $B -le 252; $B++) {
            For ($C=250; $C -le 252; $C++) {
    		    $color1 = ("DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White" | Get-Random)
    		    Write-Host -Foreground $color1 1.$A.$B.$C 
    		}
    	}
    }


    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 6:01 PM
  • Your code:

      ($color1 = "DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White") | Get-Random

    What is in $color1?


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 6:02 PM
  • I also simplified your code by taking the array assignment out of the loop.  Why recreate the same array each time.


    \_(ツ)_/

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:44 PM
    Thursday, July 23, 2015 6:03 PM
  • In your code $color1 is an array of all the colors. If you only wanted one random color you need to remove the brackets like so: 

    $color1 = "DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White" | Get-Random

    • Marked as answer by BlankMonkey Thursday, July 23, 2015 6:32 PM
    Thursday, July 23, 2015 6:03 PM
  • AHH HHAA

    PS C:\Windows\system32> ($color1 = "DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGr
    ay","Blue","Green","Cyan","Red","Magenta","Yellow","White") | Get-Random
    Magenta
    PS C:\Windows\system32> $color1
    DarkBlue
    DarkGreen
    DarkCyan
    DarkRed
    DarkMagenta
    DarkYellow
    Gray
    DarkGray
    Blue
    Green
    Cyan
    Red
    Magenta
    Yellow
    White


    BlankMonkey

    Thursday, July 23, 2015 6:15 PM
  • but then why are my results looking correct, but not seeing the value as a change......

    DarkYellow
    1.5.7.244
    Red
    1.5.7.245
    White
    1.5.7.246
    Magenta

    Is it that the command is being ignored because it doesn't have a valid value (an array)?

    But then why does it print the color value?

    ohhhhhhhh!!

    The entry " $varable" will return the value, in this case a value from the $color1 array.

    So what I am seeing is the varable being returned (from the array), and then the write-host command with a wrong value


    BlankMonkey


    • Edited by BlankMonkey Thursday, July 23, 2015 6:24 PM correction
    Thursday, July 23, 2015 6:19 PM
  • EPIC

    :)

    I wish I could give more then 1 point

    Thank you!


    BlankMonkey

    Thursday, July 23, 2015 6:32 PM
  • EPIC

    :)

    I wish I could give more then 1 point

    Thank you!


    BlankMonkey

    If you mean you wish you could mark more than one answer, you can. There's no restriction on the number of posts you can mark.


    Thursday, July 23, 2015 6:37 PM
  • :):)

    BlankMonkey

    Thursday, July 23, 2015 6:44 PM