Powershell arrays - Get upper and lower index value before joining
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
add a comment |
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
add a comment |
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
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
arrays powershell indexing find mergesort
asked Nov 13 '18 at 11:25
PhilippePhilippe
296
296
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
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
add a comment |
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
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
add a comment |
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
);
);
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%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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
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%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
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