2

I have the following function in Powershell. It always returns False and also the sorting of the arrays doesn't work within the function while it works in the console. The function is

# $a = [121, 144, 19, 161, 19, 144, 19, 11]
# $b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

function comp($a, $b) {
    if( -Not ($a.length -Eq $b.length) || -Not $a || -Not $b) {
        return $false
    }
    $a = $a | sort 
    $b = $b | sort 
    # Adding echo statements here to check if $a and $b are sorted tells that both are NOT sorted despite assigning them to the same variable
    # Assigning the sorted arrays to other variables such as $x and $y again doesn't solve the problem. $x and $y also have the unsorted values
    for($i=0;$i -lt $a.length;$i++) {
        if( -Not ($b[$i] -Eq $a[$i]*$a[$i])) {
            return $false
        }
    }
    return $true
}

Note: $a and $b in the top are initialized without [ and ] and it is just provided to give emphasis that they are arrays.

The above function returns False while it must be True. I then tried this

function comp($a, $b) {
    if( -Not ($a.length -Eq $b.length) || -Not $a || -Not $b) {
        return $false
    }

    for($i=0;$i -lt $a.length;$i++) {
        $flag = $false
        for($j=0;$j -lt $b.length; $j++) {
            if($b[$j] -Eq $a[$i]*$a[$i]) {
                $flag = $true
                # Never gets into this i.e. never executed
                break;
            }
        }
        if( -Not $flag) {
            return $flag
        }
    }
    return $true
}

But this works on the console when run without a function. Please see the image below Powershell Output

It didn't return False. And hence, the output is True which is correct

Now see the output for the above functions Function 1 powershell

Now for the second one Function 2 powershell

What is wrong here?

Suzuna Minami
  • 391
  • 2
  • 11
  • What are you actually trying to do? Your script does not run as shown. Double pipes etc – Scepticalist Jun 20 '21 at 09:39
  • Just checking if all the elements of array `b` is a square of any of the number in array `a` where arrays `a` and `b` have distinct elements themselves. – Suzuna Minami Jun 20 '21 at 09:41
  • 2
    In short: PowerShell functions, cmdlets, scripts, and external programs must be invoked _like shell commands_ - `foo arg1 arg2` - _not_ like C# methods - `foo('arg1', 'arg2')`. If you use `,` to separate arguments, you'll construct an _array_ that a command sees as a _single argument_. To prevent accidental use of method syntax, use [`Set-StrictMode -Version 2`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher, but note its other effects. See [this answer](https://stackoverflow.com/a/65208621/45375) for more information. – mklement0 Jun 20 '21 at 12:44
  • If passing the parameters must be separated by spaces, then why does it even get executed and return a value? – Suzuna Minami Jun 20 '21 at 16:12
  • 1
    You passed a jagged array (a two-element array whose elements are arrays themselves) and that array as a whole was bound to your `$a` parameter due to the mistaken use of method syntax. Nothing was bound to your `$b` parameter, but that didn't cause an error, because it is an _optional_ parameter, as all PowerShell parameters are by default. To make a parameter mandatory, you need to decorate it with `[Parameter(Mandatory)]` - see [about_Functions_Advanced](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Functions_Advanced). – mklement0 Jun 20 '21 at 16:59

1 Answers1

5

You're passing the arguments to comp incorrectly.

PowerShell's command invocation syntax expects you to pass arguments separated by whitespace, not a comma-separated list.

Change:

comp($a, $b)

to:

comp $a $b
# or
comp -a $a -b $b

See the about_Command_Syntax help topic for more information on how to invoke commands in PowerShell

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206