PDA

View Full Version : Powershell Problem, VRY SIMPLE.

SlayerApocalypse666
07-7-2012, 07:03 PM
Here is a small script i made to enter 10 values, and then come out with the Highest of these value.....but its always returning me 9 as highest value, lets say my 10 values are 1,2,3,4,5,100,1000,9
then i get 9 as my highest value. What is wrong ?

Write-Host "Enter 10 number higher than -1:" #Output Text on the line.
\$v = 0 #Variable
\$vn = 0 #Variable

for (\$a = 1; \$a -le 10; \$a++) { #For statement
\$vn = Read-Host "Enter number # " \$a #Output "enter value number \$a" then get the value \$vn from keyboard.
if (\$vn -gt \$v) { \$v = \$vn } #If \$vn is higher than the value \$v then i put \$vn into \$v
}

Write-Host "The highest value is " \$v #Output "the highest value is" value \$v

I i enter the value, 100,200,300,800,1000,900,768,654,543 my highest value will be 900 , i noticed that if the number dont have the same digital lenght, they gont get compared prolerly. Vry weird.

I fixed my problem by changing my first 2 varibles from \$v and \$nv to [int]\$v and [int]\$nv thx alot megamon for your answer.

megamon88
07-7-2012, 07:09 PM
I don't know powershell, but from what I can tell, it looks like your variable \$vn isn't reading the number with index \$a on the list, but rather taking the actual value of \$a. That's why your highest value is always 9, since \$a counts up from 0 to 9.

Sorry if this doesn't help much, don't know the language :P