Compare keys of array in RUBY
I have this structure:
$ArrayX = [8349310431,8349314513,......]
$ArrayY = [667984788,667987788,......]
$ArrayZ = [148507632380,153294624079,.....]
$range_map = $ArrayX.zip([$ArrayY.map(&:to_i),
$ArrayZ.map(&:to_i)].transpose).sort
puts $range_map =[8349310431=>[667984788, 148507632380],
8349314513=>[667987788, 153294624079]
I need the key to be compared with the rest of the keys and if the subtraction between keys is lower than 100, that key to print
ruby hash
add a comment |
I have this structure:
$ArrayX = [8349310431,8349314513,......]
$ArrayY = [667984788,667987788,......]
$ArrayZ = [148507632380,153294624079,.....]
$range_map = $ArrayX.zip([$ArrayY.map(&:to_i),
$ArrayZ.map(&:to_i)].transpose).sort
puts $range_map =[8349310431=>[667984788, 148507632380],
8349314513=>[667987788, 153294624079]
I need the key to be compared with the rest of the keys and if the subtraction between keys is lower than 100, that key to print
ruby hash
5
Don't use$
for variables, provide correct arrays (currently they're empty), provide correct output (it's currently an array in hash, which just doesn't work).
– Marcin Kołodziej
Nov 14 '18 at 3:09
Your$range_map
is an Array, not a Hash. It is still very unclear what you expect the output to be. If you provide a Minimal, Complete and Verifiable Example, you will receive a proper answer.
– Marcin Kołodziej
Nov 14 '18 at 3:29
Please don’t add unnecessary commentary in your question titles. If you really feel the need to apologize for the title then you haven’t spent enough time being thoughtful and should slow down before submitting your question. Please read stackoverflow.com/help/how-to-ask for more information on effective titles.
– anothermh
Nov 14 '18 at 5:44
add a comment |
I have this structure:
$ArrayX = [8349310431,8349314513,......]
$ArrayY = [667984788,667987788,......]
$ArrayZ = [148507632380,153294624079,.....]
$range_map = $ArrayX.zip([$ArrayY.map(&:to_i),
$ArrayZ.map(&:to_i)].transpose).sort
puts $range_map =[8349310431=>[667984788, 148507632380],
8349314513=>[667987788, 153294624079]
I need the key to be compared with the rest of the keys and if the subtraction between keys is lower than 100, that key to print
ruby hash
I have this structure:
$ArrayX = [8349310431,8349314513,......]
$ArrayY = [667984788,667987788,......]
$ArrayZ = [148507632380,153294624079,.....]
$range_map = $ArrayX.zip([$ArrayY.map(&:to_i),
$ArrayZ.map(&:to_i)].transpose).sort
puts $range_map =[8349310431=>[667984788, 148507632380],
8349314513=>[667987788, 153294624079]
I need the key to be compared with the rest of the keys and if the subtraction between keys is lower than 100, that key to print
ruby hash
ruby hash
edited Nov 14 '18 at 5:40
anothermh
3,26831632
3,26831632
asked Nov 14 '18 at 3:04
Alejandro RodriguezAlejandro Rodriguez
64
64
5
Don't use$
for variables, provide correct arrays (currently they're empty), provide correct output (it's currently an array in hash, which just doesn't work).
– Marcin Kołodziej
Nov 14 '18 at 3:09
Your$range_map
is an Array, not a Hash. It is still very unclear what you expect the output to be. If you provide a Minimal, Complete and Verifiable Example, you will receive a proper answer.
– Marcin Kołodziej
Nov 14 '18 at 3:29
Please don’t add unnecessary commentary in your question titles. If you really feel the need to apologize for the title then you haven’t spent enough time being thoughtful and should slow down before submitting your question. Please read stackoverflow.com/help/how-to-ask for more information on effective titles.
– anothermh
Nov 14 '18 at 5:44
add a comment |
5
Don't use$
for variables, provide correct arrays (currently they're empty), provide correct output (it's currently an array in hash, which just doesn't work).
– Marcin Kołodziej
Nov 14 '18 at 3:09
Your$range_map
is an Array, not a Hash. It is still very unclear what you expect the output to be. If you provide a Minimal, Complete and Verifiable Example, you will receive a proper answer.
– Marcin Kołodziej
Nov 14 '18 at 3:29
Please don’t add unnecessary commentary in your question titles. If you really feel the need to apologize for the title then you haven’t spent enough time being thoughtful and should slow down before submitting your question. Please read stackoverflow.com/help/how-to-ask for more information on effective titles.
– anothermh
Nov 14 '18 at 5:44
5
5
Don't use
$
for variables, provide correct arrays (currently they're empty), provide correct output (it's currently an array in hash, which just doesn't work).– Marcin Kołodziej
Nov 14 '18 at 3:09
Don't use
$
for variables, provide correct arrays (currently they're empty), provide correct output (it's currently an array in hash, which just doesn't work).– Marcin Kołodziej
Nov 14 '18 at 3:09
Your
$range_map
is an Array, not a Hash. It is still very unclear what you expect the output to be. If you provide a Minimal, Complete and Verifiable Example, you will receive a proper answer.– Marcin Kołodziej
Nov 14 '18 at 3:29
Your
$range_map
is an Array, not a Hash. It is still very unclear what you expect the output to be. If you provide a Minimal, Complete and Verifiable Example, you will receive a proper answer.– Marcin Kołodziej
Nov 14 '18 at 3:29
Please don’t add unnecessary commentary in your question titles. If you really feel the need to apologize for the title then you haven’t spent enough time being thoughtful and should slow down before submitting your question. Please read stackoverflow.com/help/how-to-ask for more information on effective titles.
– anothermh
Nov 14 '18 at 5:44
Please don’t add unnecessary commentary in your question titles. If you really feel the need to apologize for the title then you haven’t spent enough time being thoughtful and should slow down before submitting your question. Please read stackoverflow.com/help/how-to-ask for more information on effective titles.
– anothermh
Nov 14 '18 at 5:44
add a comment |
1 Answer
1
active
oldest
votes
I corrected your code also as per your need, and solved further,
$ArrayX = [8349310431,8349314513]
$ArrayY = [667984788,667987788]
$ArrayZ = [148507632380,153294624079]
$range_map = $ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort
$ArrayX = [8349310431,8349314513]
=> [8349310431, 8349314513]
$ArrayY = [667984788,667987788]
=> [667984788, 667987788]
$ArrayZ = [148507632380,153294624079]
=> [148507632380, 153294624079]
$range_map = Hash[$ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort]
=> 8349310431=>[667984788, 148507632380], 8349314513=>[667987788, 153294624079]
keys = $range_map.keys
valid_keys = keys.select k
$range_map.slice(*valid_keys)
If particular key is having difference more than 100 with one of rest of keys then it will be valid for filtering.
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%2f53292598%2fcompare-keys-of-array-in-ruby%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I corrected your code also as per your need, and solved further,
$ArrayX = [8349310431,8349314513]
$ArrayY = [667984788,667987788]
$ArrayZ = [148507632380,153294624079]
$range_map = $ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort
$ArrayX = [8349310431,8349314513]
=> [8349310431, 8349314513]
$ArrayY = [667984788,667987788]
=> [667984788, 667987788]
$ArrayZ = [148507632380,153294624079]
=> [148507632380, 153294624079]
$range_map = Hash[$ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort]
=> 8349310431=>[667984788, 148507632380], 8349314513=>[667987788, 153294624079]
keys = $range_map.keys
valid_keys = keys.select k
$range_map.slice(*valid_keys)
If particular key is having difference more than 100 with one of rest of keys then it will be valid for filtering.
add a comment |
I corrected your code also as per your need, and solved further,
$ArrayX = [8349310431,8349314513]
$ArrayY = [667984788,667987788]
$ArrayZ = [148507632380,153294624079]
$range_map = $ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort
$ArrayX = [8349310431,8349314513]
=> [8349310431, 8349314513]
$ArrayY = [667984788,667987788]
=> [667984788, 667987788]
$ArrayZ = [148507632380,153294624079]
=> [148507632380, 153294624079]
$range_map = Hash[$ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort]
=> 8349310431=>[667984788, 148507632380], 8349314513=>[667987788, 153294624079]
keys = $range_map.keys
valid_keys = keys.select k
$range_map.slice(*valid_keys)
If particular key is having difference more than 100 with one of rest of keys then it will be valid for filtering.
add a comment |
I corrected your code also as per your need, and solved further,
$ArrayX = [8349310431,8349314513]
$ArrayY = [667984788,667987788]
$ArrayZ = [148507632380,153294624079]
$range_map = $ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort
$ArrayX = [8349310431,8349314513]
=> [8349310431, 8349314513]
$ArrayY = [667984788,667987788]
=> [667984788, 667987788]
$ArrayZ = [148507632380,153294624079]
=> [148507632380, 153294624079]
$range_map = Hash[$ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort]
=> 8349310431=>[667984788, 148507632380], 8349314513=>[667987788, 153294624079]
keys = $range_map.keys
valid_keys = keys.select k
$range_map.slice(*valid_keys)
If particular key is having difference more than 100 with one of rest of keys then it will be valid for filtering.
I corrected your code also as per your need, and solved further,
$ArrayX = [8349310431,8349314513]
$ArrayY = [667984788,667987788]
$ArrayZ = [148507632380,153294624079]
$range_map = $ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort
$ArrayX = [8349310431,8349314513]
=> [8349310431, 8349314513]
$ArrayY = [667984788,667987788]
=> [667984788, 667987788]
$ArrayZ = [148507632380,153294624079]
=> [148507632380, 153294624079]
$range_map = Hash[$ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort]
=> 8349310431=>[667984788, 148507632380], 8349314513=>[667987788, 153294624079]
keys = $range_map.keys
valid_keys = keys.select k
$range_map.slice(*valid_keys)
If particular key is having difference more than 100 with one of rest of keys then it will be valid for filtering.
answered Nov 14 '18 at 5:30
rayray
2,3891623
2,3891623
add a comment |
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%2f53292598%2fcompare-keys-of-array-in-ruby%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
5
Don't use
$
for variables, provide correct arrays (currently they're empty), provide correct output (it's currently an array in hash, which just doesn't work).– Marcin Kołodziej
Nov 14 '18 at 3:09
Your
$range_map
is an Array, not a Hash. It is still very unclear what you expect the output to be. If you provide a Minimal, Complete and Verifiable Example, you will receive a proper answer.– Marcin Kołodziej
Nov 14 '18 at 3:29
Please don’t add unnecessary commentary in your question titles. If you really feel the need to apologize for the title then you haven’t spent enough time being thoughtful and should slow down before submitting your question. Please read stackoverflow.com/help/how-to-ask for more information on effective titles.
– anothermh
Nov 14 '18 at 5:44