none
powershell script error adding integers RRS feed

  • Question

  • Hi All, I am trying to add integers but the script is not running. I am using variable "a" with the same line of thinking as

    $arg in $args

    $a in $as

    #  create a script thats adds numbers using foreach
    # put an if statement if the numbers are not valid

    My script begins here:

       set-psdebug -strict

        $total = 0
         

    foreach($a in $as)

                    {
                   if ( $a -as[int] -eq $null)

                { Write-host "this is not a valid number"
                    exit 1
                 }

                    $total += $a

                  Write-host "The total is $total"
                  }

    I am calling my script with the following line: .\finalexam\foreachadd.ps1 6 6 i

    The error is :

    The variable '$as' cannot be retrieved because it has not been set.
    At C:\cmd.practise\finalexam\foreachadd.ps1:8 char:15
    + foreach($a in $as)
    +               ~~~
        + CategoryInfo          : InvalidOperation: (as:String) [], RuntimeException
        + FullyQualifiedErrorId : VariableIsUndefined


    Thank-you

    SQL 75

    (LearningPowerhsell)

    Tuesday, April 7, 2015 4:30 PM

Answers

  • You need to go back and study the very initial basics of PowerShell.  You are repeatedly guessing wrong and misunderstanding how this works. Without the fundamentals you will be forever lost.

    Try to understand why and how the following works.  Read the basics of using PowerShell variables and loops.

    $as=1,2,'x',4,5,'z' $total=0 foreach($a in $as){ if($a -as [int]){         $total+=$a
    }else{         Write-host "$a cannot be converted to a valid integer 32"
    } } Write-host "The total is $total"

    You also need to learn how to format your code.  Tat would make it easier for you too see your mistakes.


    \_(ツ)_/





    • Edited by jrv Tuesday, April 7, 2015 6:27 PM
    • Marked as answer by SQL75 Tuesday, April 7, 2015 7:45 PM
    Tuesday, April 7, 2015 6:23 PM
  • Now we can do this:

    Test all possible kinds of numbers:

    function Test-Numeric{
        Param( $x )
        return $x -is [byte]  -or $x -is [int16]  -or $x -is [int32]  -or $x -is [int64]  `
           -or $x -is [sbyte] -or $x -is [uint16] -or $x -is [uint32] -or $x -is [uint64] `
           -or $x -is [float] -or $x -is [double] -or $x -is [decimal]
    }
    


    \_(ツ)_/

    • Marked as answer by SQL75 Tuesday, April 7, 2015 7:45 PM
    Tuesday, April 7, 2015 6:38 PM

All replies

  • Why are you using $args?

    Use named parameters.

    (I believe this is the third time I have asked you this question in the last two days.)


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 7, 2015 4:49 PM
    Moderator
  • Bill, this time I didn't use $args. I changed it to a named variable $a. I am not sure what the error is.

    Regards

    SQL 75

    Tuesday, April 7, 2015 6:22 PM
  • You need to go back and study the very initial basics of PowerShell.  You are repeatedly guessing wrong and misunderstanding how this works. Without the fundamentals you will be forever lost.

    Try to understand why and how the following works.  Read the basics of using PowerShell variables and loops.

    $as=1,2,'x',4,5,'z' $total=0 foreach($a in $as){ if($a -as [int]){         $total+=$a
    }else{         Write-host "$a cannot be converted to a valid integer 32"
    } } Write-host "The total is $total"

    You also need to learn how to format your code.  Tat would make it easier for you too see your mistakes.


    \_(ツ)_/





    • Edited by jrv Tuesday, April 7, 2015 6:27 PM
    • Marked as answer by SQL75 Tuesday, April 7, 2015 7:45 PM
    Tuesday, April 7, 2015 6:23 PM
  • I agree with jrv. Guesswork is going to be counterproductive and you will waste a lot of time on things that don't work, as well as giving you wrong ideas about how things actually work.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 7, 2015 6:31 PM
    Moderator
  • Your idea of numbers ia also an issue.  THe int test will test if the obect can be converted to an int32.  It does not check it as a valid number.

    Here is a number:

    $num=[int64]::MaxValue

    $num=[int64]::MaxValue
    $num -as [int]
    if($num -as [int]){'number'}else{'NaN'}

    The test reports it is not a number but it is.

    Numbers are general.  [int] is specific.  It is a "kind of" a number and does not represent all numbers.


    \_(ツ)_/

    Tuesday, April 7, 2015 6:32 PM
  • Now we can do this:

    Test all possible kinds of numbers:

    function Test-Numeric{
        Param( $x )
        return $x -is [byte]  -or $x -is [int16]  -or $x -is [int32]  -or $x -is [int64]  `
           -or $x -is [sbyte] -or $x -is [uint16] -or $x -is [uint32] -or $x -is [uint64] `
           -or $x -is [float] -or $x -is [double] -or $x -is [decimal]
    }
    


    \_(ツ)_/

    • Marked as answer by SQL75 Tuesday, April 7, 2015 7:45 PM
    Tuesday, April 7, 2015 6:38 PM
  • Thanks jrv I will try to understand this. It is a bit confusing as I am not from math background.

    Regards

    SQL 75

    Tuesday, April 7, 2015 7:46 PM
  • Thanks jrv I will try to understand this. It is a bit confusing as I am not from math background.

    It is junior high school math. It is not college math. Number are absolutely required for technicians. You must understand data types and why? You cannot even be a good user administrator without knowing the difference between a int and a float. Now is your chance to catch up. If you are weak in this area then go and buy a book on 9th grade math. A review book for the SATs is usually cheap and easy to get and very easy to understand.


    \_(ツ)_/

    Tuesday, April 7, 2015 9:42 PM