Powershell Problem, VRY SIMPLE.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SlayerApocalypse666
    Banned
    • Aug 2006
    • 324

    #1

    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, 09:51 PM.
  • megamon88
    FFR Simfile Author
    FFR Simfile Author
    FFR Music Producer
    • Feb 2006
    • 2567

    #2
    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

    Comment

    • UserNameGoesHere
      FFR Veteran
      • May 2008
      • 1114

      #3
      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
      does something like a print statement (for example if $a were 1 it would print "Enter number # 1") followed by waiting for user input: the user's input is exclusively what goes to $vn

      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.
      Originally posted by Crashfan3
      Man, what would we do without bored rednecks?

      Comment

      • megamon88
        FFR Simfile Author
        FFR Simfile Author
        FFR Music Producer
        • Feb 2006
        • 2567

        #4
        Re: Powershell Problem, VRY SIMPLE.

        Whoops, that's what I get for not paying attention. Thanks for pointing it out :P

        Comment

        Working...