Determine the third octet of an IP address based on user entered site number
How much theory knowledge is actually used while playing?
How to get directions in deep space?
Why is the Sun approximated as a black body at ~ 5800 K?
Why is the "ls" command showing permissions of files in a FAT32 partition?
How do you make your own symbol when Detexify fails?
Did the UK lift the requirement for registering SIM cards?
Is there a nicer/politer/more positive alternative for "negates"?
15% tax on $7.5k earnings. Is that right?
Why is so much work done on numerical verification of the Riemann Hypothesis?
How can ping know if my host is down
Will number of steps recorded on FitBit/any fitness tracker add up distance in PokemonGo?
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
What does Apple's new App Store requirement mean
How would you translate "more" for use as an interface button?
Which was the first story featuring espers?
Microchip documentation does not label CAN buss pins on micro controller pinout diagram
What fields between the rationals and the reals allow a good notion of 2D distance?
Quoting Keynes in a lecture
The IT department bottlenecks progress, how should I handle this?
What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
A variation to the phrase "hanging over my shoulders"
Do we have to expect a queue for the shuttle from Watford Junction to Harry Potter Studio?
How to preserve electronics (computers, iPads and phones) for hundreds of years
Determine the third octet of an IP address based on user entered site number
$begingroup$
I am trying to create a function for a larger script to determine the third octet of a site IP address based off a user entered site number. I have actually come up with the function, but was hoping that someone could help me come up with something that is simpler and more elegant, instead of the multitude of nested if statements that I came up with. Here are the rules:
- For sites numbered 0001 - 0255, the third octet is the last 3 digits of the site number, less any zeroes preceding a real number (e.g. Site 0007 = 10.10.7.xxx)
- For sites numbered 0256 - 0399 and 0500 - 1999 (except 1202), the third octet is the last 2 digits of the site number less any zeroes preceding a real number (e.g. site 0315 = 10.22.15.xxx)
- For sites numbered 401 - 499, the third octet is the last two digits of the site number +100 (e.g. 0401 = 10.20.101.xxx)
- Site 1202 is an exception to the above rule, where the third octet is the last 3 digits (.202)
- For sites 5000 and above (5000 - 9999), the first digit is dropped, and then the rules above are followed. (e.g. 8248 = 10.12.48.xxx)
Here is what I have come up with. It is not very elegant, but it seems to work. Note that the first three lines will be in another function in the script, but are here for testing purposes.
$global:site = Read-Host 'What is the 4 digit site number?' #must include leading zeros
[int]$global:sitecheck=$site #need for comparasion operators
[string]$global:sitestring=$site
if ($sitecheck -le 255)
if ($sitestring.substring(0,3) -eq '000') #(i.e. 0001 - 0009)
$global:thirdoct = $sitestring.substring(3,1) #(i.e. 1-9)
elseif ($sitestring.substring(0,2) -eq '00') #(i.e. 0010 - 0099)
$global:thirdoct = $sitestring.substring(2,2) #(i.e. 10 - 99)
elseif ($sitestring.substring(0,1) -eq '0') #(i.e. 0100 - 0255)
$global:thirdoct = $sitestring.substring(1,3) #(i.e. 100 - 255)
elseif ($sitecheck -ge 256 -and $sitecheck -le 399 )
if ($sitestring.substring(2,1) -eq '0')
$global:thirdoct = $sitestring.substring(3,1)
else
$global:thirdoct = $sitestring.substring(2,2)
elseif ($sitecheck -ge 401 -and $sitecheck -le 499)
$global:thirdoct = 100+$sitestring.substring(2,2)
elseif ($sitecheck -ge 501 -and $sitecheck -le 799)
if ($sitestring.substring(2,1) -eq '0')
$global:thirdoct = $sitestring.substring(3,1)
else
$global:thirdoct = $sitestring.substring(2,2)
elseif ($sitecheck -eq 1202)
$global:thirdoct = $sitestring.substring(1,3)
elseif ($sitecheck -ge 5000 -and $sitecheck -le 9999)
$site5000 = [int]$sitestring.substring(1,3)
if ($site5000 -le 255)
if ($sitestring.substring(1,2) -eq '00')
$global:thirdoct = $sitestring.substring(3,1)
elseif ($sitestring.substring(1,1) -eq '0')
$global:thirdoct = $sitestring.substring(2,2)
else
$global:thirdoct = $sitestring.substring(1,3)
elseif (($site5000 -ge 256 -and $site5000 -le 399) -or ($site5000 -ge 501 -and $site5000 -le 999))
$global:thirdoct = $sitestring.substring(2,2)
elseif ($site5000 -ge 401 -and $site5000 -le 499)
$global:thirdoct = 100+$sitestring.substring(2,2)
powershell
New contributor
$endgroup$
add a comment |
$begingroup$
I am trying to create a function for a larger script to determine the third octet of a site IP address based off a user entered site number. I have actually come up with the function, but was hoping that someone could help me come up with something that is simpler and more elegant, instead of the multitude of nested if statements that I came up with. Here are the rules:
- For sites numbered 0001 - 0255, the third octet is the last 3 digits of the site number, less any zeroes preceding a real number (e.g. Site 0007 = 10.10.7.xxx)
- For sites numbered 0256 - 0399 and 0500 - 1999 (except 1202), the third octet is the last 2 digits of the site number less any zeroes preceding a real number (e.g. site 0315 = 10.22.15.xxx)
- For sites numbered 401 - 499, the third octet is the last two digits of the site number +100 (e.g. 0401 = 10.20.101.xxx)
- Site 1202 is an exception to the above rule, where the third octet is the last 3 digits (.202)
- For sites 5000 and above (5000 - 9999), the first digit is dropped, and then the rules above are followed. (e.g. 8248 = 10.12.48.xxx)
Here is what I have come up with. It is not very elegant, but it seems to work. Note that the first three lines will be in another function in the script, but are here for testing purposes.
$global:site = Read-Host 'What is the 4 digit site number?' #must include leading zeros
[int]$global:sitecheck=$site #need for comparasion operators
[string]$global:sitestring=$site
if ($sitecheck -le 255)
if ($sitestring.substring(0,3) -eq '000') #(i.e. 0001 - 0009)
$global:thirdoct = $sitestring.substring(3,1) #(i.e. 1-9)
elseif ($sitestring.substring(0,2) -eq '00') #(i.e. 0010 - 0099)
$global:thirdoct = $sitestring.substring(2,2) #(i.e. 10 - 99)
elseif ($sitestring.substring(0,1) -eq '0') #(i.e. 0100 - 0255)
$global:thirdoct = $sitestring.substring(1,3) #(i.e. 100 - 255)
elseif ($sitecheck -ge 256 -and $sitecheck -le 399 )
if ($sitestring.substring(2,1) -eq '0')
$global:thirdoct = $sitestring.substring(3,1)
else
$global:thirdoct = $sitestring.substring(2,2)
elseif ($sitecheck -ge 401 -and $sitecheck -le 499)
$global:thirdoct = 100+$sitestring.substring(2,2)
elseif ($sitecheck -ge 501 -and $sitecheck -le 799)
if ($sitestring.substring(2,1) -eq '0')
$global:thirdoct = $sitestring.substring(3,1)
else
$global:thirdoct = $sitestring.substring(2,2)
elseif ($sitecheck -eq 1202)
$global:thirdoct = $sitestring.substring(1,3)
elseif ($sitecheck -ge 5000 -and $sitecheck -le 9999)
$site5000 = [int]$sitestring.substring(1,3)
if ($site5000 -le 255)
if ($sitestring.substring(1,2) -eq '00')
$global:thirdoct = $sitestring.substring(3,1)
elseif ($sitestring.substring(1,1) -eq '0')
$global:thirdoct = $sitestring.substring(2,2)
else
$global:thirdoct = $sitestring.substring(1,3)
elseif (($site5000 -ge 256 -and $site5000 -le 399) -or ($site5000 -ge 501 -and $site5000 -le 999))
$global:thirdoct = $sitestring.substring(2,2)
elseif ($site5000 -ge 401 -and $site5000 -le 499)
$global:thirdoct = 100+$sitestring.substring(2,2)
powershell
New contributor
$endgroup$
add a comment |
$begingroup$
I am trying to create a function for a larger script to determine the third octet of a site IP address based off a user entered site number. I have actually come up with the function, but was hoping that someone could help me come up with something that is simpler and more elegant, instead of the multitude of nested if statements that I came up with. Here are the rules:
- For sites numbered 0001 - 0255, the third octet is the last 3 digits of the site number, less any zeroes preceding a real number (e.g. Site 0007 = 10.10.7.xxx)
- For sites numbered 0256 - 0399 and 0500 - 1999 (except 1202), the third octet is the last 2 digits of the site number less any zeroes preceding a real number (e.g. site 0315 = 10.22.15.xxx)
- For sites numbered 401 - 499, the third octet is the last two digits of the site number +100 (e.g. 0401 = 10.20.101.xxx)
- Site 1202 is an exception to the above rule, where the third octet is the last 3 digits (.202)
- For sites 5000 and above (5000 - 9999), the first digit is dropped, and then the rules above are followed. (e.g. 8248 = 10.12.48.xxx)
Here is what I have come up with. It is not very elegant, but it seems to work. Note that the first three lines will be in another function in the script, but are here for testing purposes.
$global:site = Read-Host 'What is the 4 digit site number?' #must include leading zeros
[int]$global:sitecheck=$site #need for comparasion operators
[string]$global:sitestring=$site
if ($sitecheck -le 255)
if ($sitestring.substring(0,3) -eq '000') #(i.e. 0001 - 0009)
$global:thirdoct = $sitestring.substring(3,1) #(i.e. 1-9)
elseif ($sitestring.substring(0,2) -eq '00') #(i.e. 0010 - 0099)
$global:thirdoct = $sitestring.substring(2,2) #(i.e. 10 - 99)
elseif ($sitestring.substring(0,1) -eq '0') #(i.e. 0100 - 0255)
$global:thirdoct = $sitestring.substring(1,3) #(i.e. 100 - 255)
elseif ($sitecheck -ge 256 -and $sitecheck -le 399 )
if ($sitestring.substring(2,1) -eq '0')
$global:thirdoct = $sitestring.substring(3,1)
else
$global:thirdoct = $sitestring.substring(2,2)
elseif ($sitecheck -ge 401 -and $sitecheck -le 499)
$global:thirdoct = 100+$sitestring.substring(2,2)
elseif ($sitecheck -ge 501 -and $sitecheck -le 799)
if ($sitestring.substring(2,1) -eq '0')
$global:thirdoct = $sitestring.substring(3,1)
else
$global:thirdoct = $sitestring.substring(2,2)
elseif ($sitecheck -eq 1202)
$global:thirdoct = $sitestring.substring(1,3)
elseif ($sitecheck -ge 5000 -and $sitecheck -le 9999)
$site5000 = [int]$sitestring.substring(1,3)
if ($site5000 -le 255)
if ($sitestring.substring(1,2) -eq '00')
$global:thirdoct = $sitestring.substring(3,1)
elseif ($sitestring.substring(1,1) -eq '0')
$global:thirdoct = $sitestring.substring(2,2)
else
$global:thirdoct = $sitestring.substring(1,3)
elseif (($site5000 -ge 256 -and $site5000 -le 399) -or ($site5000 -ge 501 -and $site5000 -le 999))
$global:thirdoct = $sitestring.substring(2,2)
elseif ($site5000 -ge 401 -and $site5000 -le 499)
$global:thirdoct = 100+$sitestring.substring(2,2)
powershell
New contributor
$endgroup$
I am trying to create a function for a larger script to determine the third octet of a site IP address based off a user entered site number. I have actually come up with the function, but was hoping that someone could help me come up with something that is simpler and more elegant, instead of the multitude of nested if statements that I came up with. Here are the rules:
- For sites numbered 0001 - 0255, the third octet is the last 3 digits of the site number, less any zeroes preceding a real number (e.g. Site 0007 = 10.10.7.xxx)
- For sites numbered 0256 - 0399 and 0500 - 1999 (except 1202), the third octet is the last 2 digits of the site number less any zeroes preceding a real number (e.g. site 0315 = 10.22.15.xxx)
- For sites numbered 401 - 499, the third octet is the last two digits of the site number +100 (e.g. 0401 = 10.20.101.xxx)
- Site 1202 is an exception to the above rule, where the third octet is the last 3 digits (.202)
- For sites 5000 and above (5000 - 9999), the first digit is dropped, and then the rules above are followed. (e.g. 8248 = 10.12.48.xxx)
Here is what I have come up with. It is not very elegant, but it seems to work. Note that the first three lines will be in another function in the script, but are here for testing purposes.
$global:site = Read-Host 'What is the 4 digit site number?' #must include leading zeros
[int]$global:sitecheck=$site #need for comparasion operators
[string]$global:sitestring=$site
if ($sitecheck -le 255)
if ($sitestring.substring(0,3) -eq '000') #(i.e. 0001 - 0009)
$global:thirdoct = $sitestring.substring(3,1) #(i.e. 1-9)
elseif ($sitestring.substring(0,2) -eq '00') #(i.e. 0010 - 0099)
$global:thirdoct = $sitestring.substring(2,2) #(i.e. 10 - 99)
elseif ($sitestring.substring(0,1) -eq '0') #(i.e. 0100 - 0255)
$global:thirdoct = $sitestring.substring(1,3) #(i.e. 100 - 255)
elseif ($sitecheck -ge 256 -and $sitecheck -le 399 )
if ($sitestring.substring(2,1) -eq '0')
$global:thirdoct = $sitestring.substring(3,1)
else
$global:thirdoct = $sitestring.substring(2,2)
elseif ($sitecheck -ge 401 -and $sitecheck -le 499)
$global:thirdoct = 100+$sitestring.substring(2,2)
elseif ($sitecheck -ge 501 -and $sitecheck -le 799)
if ($sitestring.substring(2,1) -eq '0')
$global:thirdoct = $sitestring.substring(3,1)
else
$global:thirdoct = $sitestring.substring(2,2)
elseif ($sitecheck -eq 1202)
$global:thirdoct = $sitestring.substring(1,3)
elseif ($sitecheck -ge 5000 -and $sitecheck -le 9999)
$site5000 = [int]$sitestring.substring(1,3)
if ($site5000 -le 255)
if ($sitestring.substring(1,2) -eq '00')
$global:thirdoct = $sitestring.substring(3,1)
elseif ($sitestring.substring(1,1) -eq '0')
$global:thirdoct = $sitestring.substring(2,2)
else
$global:thirdoct = $sitestring.substring(1,3)
elseif (($site5000 -ge 256 -and $site5000 -le 399) -or ($site5000 -ge 501 -and $site5000 -le 999))
$global:thirdoct = $sitestring.substring(2,2)
elseif ($site5000 -ge 401 -and $site5000 -le 499)
$global:thirdoct = 100+$sitestring.substring(2,2)
powershell
powershell
New contributor
New contributor
New contributor
asked 1 min ago
HarlanHarlan
1
1
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Harlan is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215966%2fdetermine-the-third-octet-of-an-ip-address-based-on-user-entered-site-number%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Harlan is a new contributor. Be nice, and check out our Code of Conduct.
Harlan is a new contributor. Be nice, and check out our Code of Conduct.
Harlan is a new contributor. Be nice, and check out our Code of Conduct.
Harlan is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215966%2fdetermine-the-third-octet-of-an-ip-address-based-on-user-entered-site-number%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown