Last active
January 30, 2020 21:19
-
-
Save SQLDBAWithABeard/7a309bc3ffc61c69f87f6881fc130c3d to your computer and use it in GitHub Desktop.
strange behaviour in PowerShell Notebook with function and comment code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"kernelspec": { | |
"name": "powershell", | |
"display_name": "PowerShell" | |
}, | |
"language_info": { | |
"name": "powershell", | |
"codemirror_mode": "shell", | |
"mimetype": "text/x-sh", | |
"file_extension": ".ps1" | |
} | |
}, | |
"nbformat_minor": 2, | |
"nbformat": 4, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"The original code with the Functions defined and a comment at the end of the function definition\r\n", | |
"\r\n", | |
"**doesnt work** - means that when you try to run the cell it just sits spinning" | |
], | |
"metadata": { | |
"azdata_cell_guid": "22132f5d-8b78-4164-8771-f9f2453a5f12" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Get-LuhnChecksum {\n", | |
" <#\n", | |
" .SYNOPSIS\n", | |
" Calculates Luhn Checksum of a number\n", | |
" .DESCRIPTION\n", | |
" Uses the Luhn Algorighm to calculate the checksum of a number\n", | |
" .EXAMPLE\n", | |
" Get-LuhnChecksum -Number 111\n", | |
" (Returns a Luhn Checksum of 5)\n", | |
" .NOTES\n", | |
" Further reading: https://en.wikipedia.org/wiki/Luhn_algorithm\n", | |
" #>\n", | |
" param (\n", | |
" # Value to calculate Luhn digit from\n", | |
" [Parameter(Mandatory=$true)] [long] $Number\n", | |
" )\n", | |
" if($Number -lt 0) {throw \"Value cannot be negative.\"}\n", | |
" # convert to array of digits\n", | |
" $digits = [byte[]](($Number -split '') -ne '')\n", | |
" # From the rightmost digit, and moving left, double the value of this and every second digit.\n", | |
" # If the result of doubling is greater than 9, then add the digits together. (eg 7*2 = 14, 1+4=5).\n", | |
" # An equivalent operation to adding the digits is to subtract 9. (eg 7*2 = 14, 14-9 = 5).\n", | |
" # is the rightmost digit even or odd? Each digit matching is doubled.\n", | |
" $checkModValue = ($digits.Count - 1) % 2\n", | |
" for ($i = $digits.Count - 1; $i -ge 0; $i--) {\n", | |
" if($i % 2 -eq $checkModValue) {\n", | |
" $digits[$i] = $digits[$i] * 2\n", | |
" if ($digits[$i] -gt 9) {\n", | |
" $digits[$i] = $digits[$i] - 9\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
" # Sum all the digits.\n", | |
" $sum = 0\n", | |
" $digits | ForEach-Object {$sum += $_}\n", | |
" # Multiply the sum by 9, and take the ones digit (modulo 10) to find the Luhn digit.\n", | |
" $LuhnDigit = ($sum * 9) % 10\n", | |
" [PSCustomObject]@{\n", | |
" \"Original Number\" = $Number\n", | |
" \"Luhn Checksum\" = $LuhnDigit\n", | |
" }\n", | |
"}# Get-LuhnChecksum\n", | |
"Function Test-LuhnChecksum {\n", | |
" <#\n", | |
" .SYNOPSIS\n", | |
" Validates a number containing a Luhn Checksum\n", | |
" .DESCRIPTION\n", | |
" Uses the Luhn Algorighm to validate a number containing a Luhn Checksum\n", | |
" .EXAMPLE\n", | |
" Test-LuhnChecksum -Number 1115\n", | |
" (Returns that the number is valid)\n", | |
" .EXAMPLE\n", | |
" Test-LuhnChecksum -Number 1114\n", | |
" (Returns that the number is not valid)\n", | |
" .NOTES\n", | |
" Further reading: https://en.wikipedia.org/wiki/Luhn_algorithm\n", | |
" #>\n", | |
" param (\n", | |
" # Value to validate via Luhn Checksum\n", | |
" [Parameter(Mandatory=$true)] [long] $Number\n", | |
" )\n", | |
" if($Number -lt 0) {throw \"Value cannot be negative.\"}\n", | |
" # separate checksum digit from rest of number\n", | |
" $NumberString = $Number.ToString()\n", | |
" $LuhnDigit = [convert]::ToByte($NumberString.Substring($NumberString.Length-1,1))\n", | |
" $NumberWithoutLuhn = [convert]::ToInt64($NumberString.Substring(0, $NumberString.Length-1))\n", | |
" $o = Get-LuhnChecksum -Number $NumberWithoutLuhn\n", | |
" [PSCustomObject]@{\n", | |
" \"Value\" = $Number\n", | |
" \"IsValid\" = $o.\"Luhn Checksum\" -eq $LuhnDigit\n", | |
" }\n", | |
"}# Test-LuhnChecksum" | |
], | |
"metadata": { | |
"azdata_cell_guid": "38d8092a-10de-4224-9b50-83f6324c85bc" | |
}, | |
"outputs": [], | |
"execution_count": 0 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Take it back to basics and it does work" | |
], | |
"metadata": { | |
"azdata_cell_guid": "442cbc3f-fa84-4786-92b8-c4481bf0f0dc" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Get-LuhnChecksum {\r\n", | |
"\r\n", | |
"}\r\n", | |
"Function Test-LuhnChecksum {\r\n", | |
" \r\n", | |
"}" | |
], | |
"metadata": { | |
"azdata_cell_guid": "e461d0e0-5a09-4836-ab87-9c14f72a33b0" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "" | |
} | |
], | |
"execution_count": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Add in the comment and it doesnt work" | |
], | |
"metadata": { | |
"azdata_cell_guid": "fdf7ceef-9e99-4e7c-b542-368c34c9df7d" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Get-LuhnChecksum {\r\n", | |
" \r\n", | |
"}# Get-LuhnChecksum\r\n", | |
"Function Test-LuhnChecksum {\r\n", | |
" \r\n", | |
"}# Test-LuhnChecksum" | |
], | |
"metadata": { | |
"azdata_cell_guid": "9a714d67-5a61-42cc-8a61-8d52d6913752" | |
}, | |
"outputs": [], | |
"execution_count": 0 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Just to check - All of the code without the comment does work" | |
], | |
"metadata": { | |
"azdata_cell_guid": "3366bfbc-88bc-401b-aff0-64a19088000d" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Get-LuhnChecksum {\r\n", | |
" <#\r\n", | |
" .SYNOPSIS\r\n", | |
" Calculates Luhn Checksum of a number\r\n", | |
" .DESCRIPTION\r\n", | |
" Uses the Luhn Algorighm to calculate the checksum of a number\r\n", | |
" .EXAMPLE\r\n", | |
" Get-LuhnChecksum -Number 111\r\n", | |
" (Returns a Luhn Checksum of 5)\r\n", | |
" .NOTES\r\n", | |
" Further reading: https://en.wikipedia.org/wiki/Luhn_algorithm\r\n", | |
" #>\r\n", | |
" param (\r\n", | |
" # Value to calculate Luhn digit from\r\n", | |
" [Parameter(Mandatory=$true)] [long] $Number\r\n", | |
" )\r\n", | |
" if($Number -lt 0) {throw \"Value cannot be negative.\"}\r\n", | |
" # convert to array of digits\r\n", | |
" $digits = [byte[]](($Number -split '') -ne '')\r\n", | |
" # From the rightmost digit, and moving left, double the value of this and every second digit.\r\n", | |
" # If the result of doubling is greater than 9, then add the digits together. (eg 7*2 = 14, 1+4=5).\r\n", | |
" # An equivalent operation to adding the digits is to subtract 9. (eg 7*2 = 14, 14-9 = 5).\r\n", | |
" # is the rightmost digit even or odd? Each digit matching is doubled.\r\n", | |
" $checkModValue = ($digits.Count - 1) % 2\r\n", | |
" for ($i = $digits.Count - 1; $i -ge 0; $i--) {\r\n", | |
" if($i % 2 -eq $checkModValue) {\r\n", | |
" $digits[$i] = $digits[$i] * 2\r\n", | |
" if ($digits[$i] -gt 9) {\r\n", | |
" $digits[$i] = $digits[$i] - 9\r\n", | |
" }\r\n", | |
" }\r\n", | |
" }\r\n", | |
" # Sum all the digits.\r\n", | |
" $sum = 0\r\n", | |
" $digits | ForEach-Object {$sum += $_}\r\n", | |
" # Multiply the sum by 9, and take the ones digit (modulo 10) to find the Luhn digit.\r\n", | |
" $LuhnDigit = ($sum * 9) % 10\r\n", | |
" [PSCustomObject]@{\r\n", | |
" \"Original Number\" = $Number\r\n", | |
" \"Luhn Checksum\" = $LuhnDigit\r\n", | |
" }\r\n", | |
"}\r\n", | |
"Function Test-LuhnChecksum {\r\n", | |
" <#\r\n", | |
" .SYNOPSIS\r\n", | |
" Validates a number containing a Luhn Checksum\r\n", | |
" .DESCRIPTION\r\n", | |
" Uses the Luhn Algorighm to validate a number containing a Luhn Checksum\r\n", | |
" .EXAMPLE\r\n", | |
" Test-LuhnChecksum -Number 1115\r\n", | |
" (Returns that the number is valid)\r\n", | |
" .EXAMPLE\r\n", | |
" Test-LuhnChecksum -Number 1114\r\n", | |
" (Returns that the number is not valid)\r\n", | |
" .NOTES\r\n", | |
" Further reading: https://en.wikipedia.org/wiki/Luhn_algorithm\r\n", | |
" #>\r\n", | |
" param (\r\n", | |
" # Value to validate via Luhn Checksum\r\n", | |
" [Parameter(Mandatory=$true)] [long] $Number\r\n", | |
" )\r\n", | |
" if($Number -lt 0) {throw \"Value cannot be negative.\"}\r\n", | |
" # separate checksum digit from rest of number\r\n", | |
" $NumberString = $Number.ToString()\r\n", | |
" $LuhnDigit = [convert]::ToByte($NumberString.Substring($NumberString.Length-1,1))\r\n", | |
" $NumberWithoutLuhn = [convert]::ToInt64($NumberString.Substring(0, $NumberString.Length-1))\r\n", | |
" $o = Get-LuhnChecksum -Number $NumberWithoutLuhn\r\n", | |
" [PSCustomObject]@{\r\n", | |
" \"Value\" = $Number\r\n", | |
" \"IsValid\" = $o.\"Luhn Checksum\" -eq $LuhnDigit\r\n", | |
" }\r\n", | |
"}" | |
], | |
"metadata": { | |
"azdata_cell_guid": "b7e6506b-2d63-4907-8c05-a8c1e3f6ed23" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "" | |
} | |
], | |
"execution_count": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Add a space after the } doesnt work" | |
], | |
"metadata": { | |
"azdata_cell_guid": "d353385c-72c4-4449-8ff1-c810550c273d" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Get-LuhnChecksum {\r\n", | |
" \r\n", | |
"} # Get-LuhnChecksum\r\n", | |
"Function Test-LuhnChecksum {\r\n", | |
"\r\n", | |
"} # Test-LuhnChecksum" | |
], | |
"metadata": { | |
"azdata_cell_guid": "a0acae4c-870e-4b96-94da-7f8897a49a8d" | |
}, | |
"outputs": [], | |
"execution_count": 0 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Put the comment on its own line also doesnt work" | |
], | |
"metadata": { | |
"azdata_cell_guid": "c972ce86-146d-412d-96ed-4fd6fc440a3e" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Get-LuhnChecksum {\r\n", | |
" \r\n", | |
"}\r\n", | |
"\r\n", | |
"# Get-LuhnChecksum\r\n", | |
"\r\n", | |
"Function Test-LuhnChecksum {\r\n", | |
" \r\n", | |
"}\r\n", | |
"\r\n", | |
"# Test-LuhnChecksum" | |
], | |
"metadata": { | |
"azdata_cell_guid": "007f228f-c449-46e4-ad5c-50ef5e1333f7" | |
}, | |
"outputs": [], | |
"execution_count": 0 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Use <# #> for the comment - does work" | |
], | |
"metadata": { | |
"azdata_cell_guid": "2a933040-640a-4265-8deb-47d6f1d267f2" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Get-LuhnChecksum {\n", | |
" \n", | |
"}\n", | |
"\n", | |
" <# Get-LuhnChecksum #>\n", | |
"Function Test-LuhnChecksum {\n", | |
" \n", | |
"}\n", | |
"\n", | |
"<# Test-LuhnChecksum #>" | |
], | |
"metadata": { | |
"azdata_cell_guid": "74f0704c-e005-4624-9a44-e782272cdff9" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"text": "", | |
"output_type": "stream" | |
} | |
], | |
"execution_count": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"azdata_cell_guid": "01a58c7c-096e-4990-9827-77adf44fad10" | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment