Answered by:
powershell script error adding integers
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:
setpsdebug strict
$total = 0
foreach($a in $as)
{
if ( $a as[int] eq $null){ Writehost "this is not a valid number"
exit 1
}
$total += $a
Writehost "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
Thankyou
SQL 75
(LearningPowerhsell)
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.
You also need to learn how to format your code. Tat would make it easier for you too see your mistakes.$as=1,2,'x',4,5,'z' $total=0 foreach($a in $as){ if($a as [int]){ $total+=$a
}else{ Writehost "$a cannot be converted to a valid integer 32"
} } Writehost "The total is $total"
\_(ツ)_/

Now we can do this:
Test all possible kinds of numbers:
function TestNumeric{ 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
All replies



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.
You also need to learn how to format your code. Tat would make it easier for you too see your mistakes.$as=1,2,'x',4,5,'z' $total=0 foreach($a in $as){ if($a as [int]){ $total+=$a
}else{ Writehost "$a cannot be converted to a valid integer 32"
} } Writehost "The total is $total"
\_(ツ)_/


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.
\_(ツ)_/

Now we can do this:
Test all possible kinds of numbers:
function TestNumeric{ 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


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.
\_(ツ)_/