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














0












$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)










share







New contributor




Harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$
















    0












    $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)










    share







    New contributor




    Harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      0












      0








      0





      $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)










      share







      New contributor




      Harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $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





      share







      New contributor




      Harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 min ago









      HarlanHarlan

      1




      1




      New contributor




      Harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          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.









          draft saved

          draft discarded


















          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.









          draft saved

          draft discarded


















          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          कुँवर स्रोत दिक्चालन सूची"कुँवर""राणा कुँवरके वंशावली"

          Why is a white electrical wire connected to 2 black wires?How to wire a light fixture with 3 white wires in box?How should I wire a ceiling fan when there's only three wires in the box?Two white, two black, two ground, and red wire in ceiling box connected to switchWhy is there a white wire connected to multiple black wires in my light box?How to wire a light with two white wires and one black wireReplace light switch connected to a power outlet with dimmer - two black wires to one black and redHow to wire a light with multiple black/white/green wires from the ceiling?Ceiling box has 2 black and white wires but fan/ light only has 1 of eachWhy neutral wire connected to load wire?Switch with 2 black, 2 white, 2 ground and 1 red wire connected to ceiling light and a receptacle?

          चैत्य भूमि चित्र दीर्घा सन्दर्भ बाहरी कडियाँ दिक्चालन सूची"Chaitya Bhoomi""Chaitya Bhoomi: Statue of Equality in India""Dadar Chaitya Bhoomi: Statue of Equality in India""Ambedkar memorial: Centre okays transfer of Indu Mill land"चैत्यभमि