0

PowerShell's automatically presuming that the value '9e9' (in the COMMAND below) is an integer; since the value isn't surrounded by quotes.

I'm looking for any clever way to either treat all array elements/values as strings (no matter what) or force PowerShell to treat any values with an 'e' character in the middle, as a string... or, any other possible way before the its automatically casted as an integer WITHOUT having to surround the value with quotes.

COMMAND:

Get-Input -var 9e9, ba7


CODE:

function Get-Input {

    [CmdletBinding()]
    param(
        [array[]]$Vars
    )

    $Var = $Vars[0]

    write-output $Var
}

**** UPDATE ****

TessellatingHeckler has the best solution at the end of his answer. I just made one minor modification to strip spaces. I still think it's all PowerShell's fault. j/k. I really appreciate the time TessellatingHeckler to break it all down and still provide a sensible solution. Thanks TessellatingHeckler!

COMMAND:

Get-Input -vars "9e9, ba7"


NEW CODE:

function Get-Input {

    [CmdletBinding()]
    param(
        [string[]]$Vars
    )

    $Vars = $Vars.replace(' ', "")
    $Vars = $Vars.split(",")

    $Var = $Vars[0]

    write-output $Var

}
1
  • 1
    Declare [String[]] instead of [array[]]. Commented Sep 28, 2017 at 18:20

1 Answer 1

2

PowerShell's automatically presuming that the value '9e9' (in the COMMAND below) is an integer; since the value isn't surrounded by quotes.

Admittedly, that's got me a bit annoyed at the 'blame the tool I like' wording; 'Presume': "be arrogant or impertinent enough to do something"

It's in the language spec (section 2.3.5.12 Real Literals) that "number e number" is a way of writing a real number ([double]).

That's not PowerShell presuming anything, you're telling it to interpret it that way by using the "this is a number" syntax instead of "this is a string" syntax (quotes).

Anyway.

I'm looking for any clever way to either treat all array elements/values as strings (no matter what) [..]

before it's automatically casted as an integer WITHOUT having to surround the value with quotes.

The array value is 9000000000, if you could force it to be a string, the string would be "9000000000" - i.e. at that point, it's already too late to change it back.

In the same way that 1/2 means 0.5, you can't convert 0.5 to a string and expect to get 1/2 back out.

It's not being cast as a number, it is a number in the PowerShell language.

That sounds a bit like I'm repeating the same point, but the significance this time is that anything you do in the function, such as setting the parameter type to [string[]] is too late to make a difference, the array already has the undesired interpretation in it. Any intervention would have to happen sooner.

force PowerShell to treat any values with an 'e' character in the middle, as a string... or, any other possible way

As it scans Get-Input -var 9e9, ba7 it reads a GenericToken for 'Get-Input' which is going to turn into a command.

It hits '-' and starts reading an operator or a parameter, reads 'var' up to the space and doesn't match an operator ('-eq', '-gt', etc) so treats it as a parameter.

It reads the '9' and starts reading a number.

There's only two ways out of this - if the number is valid, it's read as a number. That happens now and you don't want it.

If the number is not valid, e.g. 7z.exe starts with a number, but is not one, then it fails, backtracks out, and treats it as a GenericToken for an argument.

So you could tack on something else to force it to be a string 9e9z. But you can't write '9e9' and have it become a string without quotes.

About the only thing you can do is use something else as a separator (not a comma) which would force the entire argument to be one string - that means no spaces either - and then split it yourself into text pieces inside the function.

function Get-Input {

    [CmdletBinding()]
    param(
        [string]$VarsText
    )

    $Vars = $VarsText.Split('.')

    $Var = $Vars[0]
    write-output $Var
}

Get-Input 9e9.bab

Which will only confuse everyone, and would likely read more helpfully if you did

Get-Input "9e9,bab,..." 

and used the comma everyone is familiar with, inside one quoted string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.