07-7-2012, 06:03 PM | #1 |
Banned
Join Date: Aug 2006
Posts: 324
|
Powershell Problem, VRY SIMPLE.
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. Last edited by SlayerApocalypse666; 07-7-2012 at 10:51 PM.. |
07-7-2012, 06:09 PM | #2 |
aka Assertive
Join Date: Feb 2006
Location: Waterloo, ON
Age: 29
Posts: 2,567
|
Re: Powershell Problem, VRY SIMPLE.
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 |
07-8-2012, 07:30 AM | #3 |
FFR Veteran
|
Re: Powershell Problem, VRY SIMPLE.
I haven't used PowerShell but that code looks correct.
megamon, the contents of $a are merely printed to the user and $a is used as a counter of the for loop itself. I see where your confusion is, but this line Code:
$vn = Read-Host "Enter number # " $a It looks like the [int]$v syntax is a kind of data binding? (Again, I've not used PowerShell myself). But it looks like it's a way to "link" certain things? It's confusing because it is too similar to the array notation in other languages. |
07-8-2012, 10:50 AM | #4 |
aka Assertive
Join Date: Feb 2006
Location: Waterloo, ON
Age: 29
Posts: 2,567
|
Re: Powershell Problem, VRY SIMPLE.
Whoops, that's what I get for not paying attention. Thanks for pointing it out :P
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|