Powershell arrays - Get upper and lower index value before joining










0















I have a two PowerShell objects that I parse: $Table1 and $Table2.
Parsing Table 1, I get a Timestamp: $TimeStamp for which I need to find the two closest values in Table 2.



$Table2 looks like this:


Price TimeStamp
----- ----------------
0.0597 1542056680.72746
0.0584 1542056650.34414
0.0555 1542056197.46668
0.0551 1542056167.28967

$TimeStamp = 1542056303
$Table2 is already sorted by TimeStamp


My goal is to get back efficiently the upper and lower indexes of $Table2 (core of my question). I will then make a linear interpolation between the two timestamps and the two prices to get the value of $Price.



The linear interpolation part is not required in the answer, this is just for context purpose.



Cheers,



Philippe










share|improve this question


























    0















    I have a two PowerShell objects that I parse: $Table1 and $Table2.
    Parsing Table 1, I get a Timestamp: $TimeStamp for which I need to find the two closest values in Table 2.



    $Table2 looks like this:


    Price TimeStamp
    ----- ----------------
    0.0597 1542056680.72746
    0.0584 1542056650.34414
    0.0555 1542056197.46668
    0.0551 1542056167.28967

    $TimeStamp = 1542056303
    $Table2 is already sorted by TimeStamp


    My goal is to get back efficiently the upper and lower indexes of $Table2 (core of my question). I will then make a linear interpolation between the two timestamps and the two prices to get the value of $Price.



    The linear interpolation part is not required in the answer, this is just for context purpose.



    Cheers,



    Philippe










    share|improve this question
























      0












      0








      0








      I have a two PowerShell objects that I parse: $Table1 and $Table2.
      Parsing Table 1, I get a Timestamp: $TimeStamp for which I need to find the two closest values in Table 2.



      $Table2 looks like this:


      Price TimeStamp
      ----- ----------------
      0.0597 1542056680.72746
      0.0584 1542056650.34414
      0.0555 1542056197.46668
      0.0551 1542056167.28967

      $TimeStamp = 1542056303
      $Table2 is already sorted by TimeStamp


      My goal is to get back efficiently the upper and lower indexes of $Table2 (core of my question). I will then make a linear interpolation between the two timestamps and the two prices to get the value of $Price.



      The linear interpolation part is not required in the answer, this is just for context purpose.



      Cheers,



      Philippe










      share|improve this question














      I have a two PowerShell objects that I parse: $Table1 and $Table2.
      Parsing Table 1, I get a Timestamp: $TimeStamp for which I need to find the two closest values in Table 2.



      $Table2 looks like this:


      Price TimeStamp
      ----- ----------------
      0.0597 1542056680.72746
      0.0584 1542056650.34414
      0.0555 1542056197.46668
      0.0551 1542056167.28967

      $TimeStamp = 1542056303
      $Table2 is already sorted by TimeStamp


      My goal is to get back efficiently the upper and lower indexes of $Table2 (core of my question). I will then make a linear interpolation between the two timestamps and the two prices to get the value of $Price.



      The linear interpolation part is not required in the answer, this is just for context purpose.



      Cheers,



      Philippe







      arrays powershell indexing find mergesort






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 11:25









      PhilippePhilippe

      296




      296






















          2 Answers
          2






          active

          oldest

          votes


















          2














          I would do it like that:



          $TimeStamp = 1542056303

          # find closest before given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -gt $TimeStamp | Select-Object -Last 1

          # find closest after given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -lt $TimeStamp | Select-Object -First 1





          share|improve this answer

























          • Since the table is sorted descending, you've to exchange -lt <=> -gt otherwise you'll get the very first and very last entry instead of the nearest.

            – LotPings
            Nov 13 '18 at 13:21






          • 1





            You're right @LotPings. I've changed it.

            – TobyU
            Nov 13 '18 at 15:21


















          1














          Imo you don't need the index if you store the row.



          • iterating the table and check if current value less or equal than $TimeStamp

          • if not store current row as $Upper

          • if -le store row as $Lower and break the foreach


          ## Q:Test20181113SO_53279995.ps1

          $TimeStamp = 1542056303
          $table2 = Import-Csv '.table2.csv' | Sort-Object TimeStamp -Descending
          $Upper = $Null
          $Lower = $Null

          ForEach ($Row in $table2)
          if([Double]$Row.TimeStamp -le $TimeStamp)
          $Lower = $Row
          Break
          else
          $Upper = $Row


          If ($Upper -and $Lower)
          $Upper
          $Lower
          "Do your interpolation"
          else
          "can't evaluate nearest values"



          Sample output



          Price TimeStamp
          ----- ---------
          0.0584 1542056650.34414
          0.0555 1542056197.46668
          Do your interpolation





          share|improve this answer

























          • Works great ! Thanks !!! I wanted to use the index for better performance, but this is not too much the issue here :-)

            – Philippe
            Nov 13 '18 at 13:08










          Your Answer






          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: "1"
          ;
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279995%2fpowershell-arrays-get-upper-and-lower-index-value-before-joining%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          I would do it like that:



          $TimeStamp = 1542056303

          # find closest before given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -gt $TimeStamp | Select-Object -Last 1

          # find closest after given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -lt $TimeStamp | Select-Object -First 1





          share|improve this answer

























          • Since the table is sorted descending, you've to exchange -lt <=> -gt otherwise you'll get the very first and very last entry instead of the nearest.

            – LotPings
            Nov 13 '18 at 13:21






          • 1





            You're right @LotPings. I've changed it.

            – TobyU
            Nov 13 '18 at 15:21















          2














          I would do it like that:



          $TimeStamp = 1542056303

          # find closest before given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -gt $TimeStamp | Select-Object -Last 1

          # find closest after given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -lt $TimeStamp | Select-Object -First 1





          share|improve this answer

























          • Since the table is sorted descending, you've to exchange -lt <=> -gt otherwise you'll get the very first and very last entry instead of the nearest.

            – LotPings
            Nov 13 '18 at 13:21






          • 1





            You're right @LotPings. I've changed it.

            – TobyU
            Nov 13 '18 at 15:21













          2












          2








          2







          I would do it like that:



          $TimeStamp = 1542056303

          # find closest before given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -gt $TimeStamp | Select-Object -Last 1

          # find closest after given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -lt $TimeStamp | Select-Object -First 1





          share|improve this answer















          I would do it like that:



          $TimeStamp = 1542056303

          # find closest before given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -gt $TimeStamp | Select-Object -Last 1

          # find closest after given timestamp
          $Table2 | Where-Object [int]$_.Timestamp -lt $TimeStamp | Select-Object -First 1






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 15:21

























          answered Nov 13 '18 at 11:45









          TobyUTobyU

          2,3221921




          2,3221921












          • Since the table is sorted descending, you've to exchange -lt <=> -gt otherwise you'll get the very first and very last entry instead of the nearest.

            – LotPings
            Nov 13 '18 at 13:21






          • 1





            You're right @LotPings. I've changed it.

            – TobyU
            Nov 13 '18 at 15:21

















          • Since the table is sorted descending, you've to exchange -lt <=> -gt otherwise you'll get the very first and very last entry instead of the nearest.

            – LotPings
            Nov 13 '18 at 13:21






          • 1





            You're right @LotPings. I've changed it.

            – TobyU
            Nov 13 '18 at 15:21
















          Since the table is sorted descending, you've to exchange -lt <=> -gt otherwise you'll get the very first and very last entry instead of the nearest.

          – LotPings
          Nov 13 '18 at 13:21





          Since the table is sorted descending, you've to exchange -lt <=> -gt otherwise you'll get the very first and very last entry instead of the nearest.

          – LotPings
          Nov 13 '18 at 13:21




          1




          1





          You're right @LotPings. I've changed it.

          – TobyU
          Nov 13 '18 at 15:21





          You're right @LotPings. I've changed it.

          – TobyU
          Nov 13 '18 at 15:21













          1














          Imo you don't need the index if you store the row.



          • iterating the table and check if current value less or equal than $TimeStamp

          • if not store current row as $Upper

          • if -le store row as $Lower and break the foreach


          ## Q:Test20181113SO_53279995.ps1

          $TimeStamp = 1542056303
          $table2 = Import-Csv '.table2.csv' | Sort-Object TimeStamp -Descending
          $Upper = $Null
          $Lower = $Null

          ForEach ($Row in $table2)
          if([Double]$Row.TimeStamp -le $TimeStamp)
          $Lower = $Row
          Break
          else
          $Upper = $Row


          If ($Upper -and $Lower)
          $Upper
          $Lower
          "Do your interpolation"
          else
          "can't evaluate nearest values"



          Sample output



          Price TimeStamp
          ----- ---------
          0.0584 1542056650.34414
          0.0555 1542056197.46668
          Do your interpolation





          share|improve this answer

























          • Works great ! Thanks !!! I wanted to use the index for better performance, but this is not too much the issue here :-)

            – Philippe
            Nov 13 '18 at 13:08















          1














          Imo you don't need the index if you store the row.



          • iterating the table and check if current value less or equal than $TimeStamp

          • if not store current row as $Upper

          • if -le store row as $Lower and break the foreach


          ## Q:Test20181113SO_53279995.ps1

          $TimeStamp = 1542056303
          $table2 = Import-Csv '.table2.csv' | Sort-Object TimeStamp -Descending
          $Upper = $Null
          $Lower = $Null

          ForEach ($Row in $table2)
          if([Double]$Row.TimeStamp -le $TimeStamp)
          $Lower = $Row
          Break
          else
          $Upper = $Row


          If ($Upper -and $Lower)
          $Upper
          $Lower
          "Do your interpolation"
          else
          "can't evaluate nearest values"



          Sample output



          Price TimeStamp
          ----- ---------
          0.0584 1542056650.34414
          0.0555 1542056197.46668
          Do your interpolation





          share|improve this answer

























          • Works great ! Thanks !!! I wanted to use the index for better performance, but this is not too much the issue here :-)

            – Philippe
            Nov 13 '18 at 13:08













          1












          1








          1







          Imo you don't need the index if you store the row.



          • iterating the table and check if current value less or equal than $TimeStamp

          • if not store current row as $Upper

          • if -le store row as $Lower and break the foreach


          ## Q:Test20181113SO_53279995.ps1

          $TimeStamp = 1542056303
          $table2 = Import-Csv '.table2.csv' | Sort-Object TimeStamp -Descending
          $Upper = $Null
          $Lower = $Null

          ForEach ($Row in $table2)
          if([Double]$Row.TimeStamp -le $TimeStamp)
          $Lower = $Row
          Break
          else
          $Upper = $Row


          If ($Upper -and $Lower)
          $Upper
          $Lower
          "Do your interpolation"
          else
          "can't evaluate nearest values"



          Sample output



          Price TimeStamp
          ----- ---------
          0.0584 1542056650.34414
          0.0555 1542056197.46668
          Do your interpolation





          share|improve this answer















          Imo you don't need the index if you store the row.



          • iterating the table and check if current value less or equal than $TimeStamp

          • if not store current row as $Upper

          • if -le store row as $Lower and break the foreach


          ## Q:Test20181113SO_53279995.ps1

          $TimeStamp = 1542056303
          $table2 = Import-Csv '.table2.csv' | Sort-Object TimeStamp -Descending
          $Upper = $Null
          $Lower = $Null

          ForEach ($Row in $table2)
          if([Double]$Row.TimeStamp -le $TimeStamp)
          $Lower = $Row
          Break
          else
          $Upper = $Row


          If ($Upper -and $Lower)
          $Upper
          $Lower
          "Do your interpolation"
          else
          "can't evaluate nearest values"



          Sample output



          Price TimeStamp
          ----- ---------
          0.0584 1542056650.34414
          0.0555 1542056197.46668
          Do your interpolation






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 12:20

























          answered Nov 13 '18 at 12:10









          LotPingsLotPings

          19k61532




          19k61532












          • Works great ! Thanks !!! I wanted to use the index for better performance, but this is not too much the issue here :-)

            – Philippe
            Nov 13 '18 at 13:08

















          • Works great ! Thanks !!! I wanted to use the index for better performance, but this is not too much the issue here :-)

            – Philippe
            Nov 13 '18 at 13:08
















          Works great ! Thanks !!! I wanted to use the index for better performance, but this is not too much the issue here :-)

          – Philippe
          Nov 13 '18 at 13:08





          Works great ! Thanks !!! I wanted to use the index for better performance, but this is not too much the issue here :-)

          – Philippe
          Nov 13 '18 at 13:08

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • 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.

          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%2fstackoverflow.com%2fquestions%2f53279995%2fpowershell-arrays-get-upper-and-lower-index-value-before-joining%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo