Combine arrays with conditions in Ruby2019 Community Moderator ElectionCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow to write a switch statement in RubyCheck if a value exists in an array in RubyLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Existence of subset with given Hausdorff dimension
How to create the Curved texte?
Sailing the cryptic seas
Employee lack of ownership
Who is flying the vertibirds?
How to deal with taxi scam when on vacation?
Why did it take so long to abandon sail after steamships were demonstrated?
Dice rolling probability game
Welcoming 2019 Pi day: How to draw the letter π?
It's a yearly task, alright
How to deal with a cynical class?
What is the significance behind "40 days" that often appears in the Bible?
how to draw discrete time diagram in tikz
Is it normal that my co-workers at a fitness company criticize my food choices?
Does Mathematica reuse previous computations?
Awsome yet unlucky path traversal
Why would a flight no longer considered airworthy be redirected like this?
Why one should not leave fingerprints on bulbs and plugs?
The difference between「N分で」and「後N分で」
Life insurance that covers only simultaneous/dual deaths
How to read the value of this capacitor?
What is a^b and (a&b)<<1?
My adviser wants to be the first author
My Graph Theory Students
Combine arrays with conditions in Ruby
2019 Community Moderator ElectionCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow to write a switch statement in RubyCheck if a value exists in an array in RubyLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
I have a class People
with three properties
class People
attr_accessor :first_name, :last_name, :age
end
And I have two arrays:
a = [p1, p2]
b = [p3, p4]
Is there any easy way to combine these two arrays in a new array and remove the item with a condition like:
p1.first_name + p1.last_name == p3.first_name + p3.last_name
And after that all the item should be belong to array a
For example
p1.first_name = "Ada"
p1.last_name = "Wang"
p1.age = 28
p2.first_name = "Leon"
p2.last_name = "S"
p2.age = 28
p3.first_name = "Ada"
p3.last_name = "Wang"
p3.age = 18
p4.first_name = "Mario"
p4.last_name = "M"
p4.age = 80
the result should be [p1] the 28 years old Ada.Wang
arrays ruby
add a comment |
I have a class People
with three properties
class People
attr_accessor :first_name, :last_name, :age
end
And I have two arrays:
a = [p1, p2]
b = [p3, p4]
Is there any easy way to combine these two arrays in a new array and remove the item with a condition like:
p1.first_name + p1.last_name == p3.first_name + p3.last_name
And after that all the item should be belong to array a
For example
p1.first_name = "Ada"
p1.last_name = "Wang"
p1.age = 28
p2.first_name = "Leon"
p2.last_name = "S"
p2.age = 28
p3.first_name = "Ada"
p3.last_name = "Wang"
p3.age = 18
p4.first_name = "Mario"
p4.last_name = "M"
p4.age = 80
the result should be [p1] the 28 years old Ada.Wang
arrays ruby
2
In your condition you comparep1
's name top3
's name. What aboutp2
andp4
? Could you give a more complete example with actual data and show the expected output?
– Stefan
Mar 7 at 13:52
@Shaggon do you want the result to be an array of persons with no duplicate first_name last_name ?
– steenslag
Mar 7 at 17:29
Yes. I want a result with no duplicate first_name last_name and all the item should be belong to array a. The result should be like[p1]
because p2 have duplicate first_name last_name with p3 and p4 is inside array b, so only p1 is what I want
– Shaggon
Mar 8 at 2:26
Thanks stenfan and steenslag. I found the result I want is(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:15
add a comment |
I have a class People
with three properties
class People
attr_accessor :first_name, :last_name, :age
end
And I have two arrays:
a = [p1, p2]
b = [p3, p4]
Is there any easy way to combine these two arrays in a new array and remove the item with a condition like:
p1.first_name + p1.last_name == p3.first_name + p3.last_name
And after that all the item should be belong to array a
For example
p1.first_name = "Ada"
p1.last_name = "Wang"
p1.age = 28
p2.first_name = "Leon"
p2.last_name = "S"
p2.age = 28
p3.first_name = "Ada"
p3.last_name = "Wang"
p3.age = 18
p4.first_name = "Mario"
p4.last_name = "M"
p4.age = 80
the result should be [p1] the 28 years old Ada.Wang
arrays ruby
I have a class People
with three properties
class People
attr_accessor :first_name, :last_name, :age
end
And I have two arrays:
a = [p1, p2]
b = [p3, p4]
Is there any easy way to combine these two arrays in a new array and remove the item with a condition like:
p1.first_name + p1.last_name == p3.first_name + p3.last_name
And after that all the item should be belong to array a
For example
p1.first_name = "Ada"
p1.last_name = "Wang"
p1.age = 28
p2.first_name = "Leon"
p2.last_name = "S"
p2.age = 28
p3.first_name = "Ada"
p3.last_name = "Wang"
p3.age = 18
p4.first_name = "Mario"
p4.last_name = "M"
p4.age = 80
the result should be [p1] the 28 years old Ada.Wang
arrays ruby
arrays ruby
edited Mar 8 at 2:49
Shaggon
asked Mar 7 at 13:43
ShaggonShaggon
84
84
2
In your condition you comparep1
's name top3
's name. What aboutp2
andp4
? Could you give a more complete example with actual data and show the expected output?
– Stefan
Mar 7 at 13:52
@Shaggon do you want the result to be an array of persons with no duplicate first_name last_name ?
– steenslag
Mar 7 at 17:29
Yes. I want a result with no duplicate first_name last_name and all the item should be belong to array a. The result should be like[p1]
because p2 have duplicate first_name last_name with p3 and p4 is inside array b, so only p1 is what I want
– Shaggon
Mar 8 at 2:26
Thanks stenfan and steenslag. I found the result I want is(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:15
add a comment |
2
In your condition you comparep1
's name top3
's name. What aboutp2
andp4
? Could you give a more complete example with actual data and show the expected output?
– Stefan
Mar 7 at 13:52
@Shaggon do you want the result to be an array of persons with no duplicate first_name last_name ?
– steenslag
Mar 7 at 17:29
Yes. I want a result with no duplicate first_name last_name and all the item should be belong to array a. The result should be like[p1]
because p2 have duplicate first_name last_name with p3 and p4 is inside array b, so only p1 is what I want
– Shaggon
Mar 8 at 2:26
Thanks stenfan and steenslag. I found the result I want is(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:15
2
2
In your condition you compare
p1
's name to p3
's name. What about p2
and p4
? Could you give a more complete example with actual data and show the expected output?– Stefan
Mar 7 at 13:52
In your condition you compare
p1
's name to p3
's name. What about p2
and p4
? Could you give a more complete example with actual data and show the expected output?– Stefan
Mar 7 at 13:52
@Shaggon do you want the result to be an array of persons with no duplicate first_name last_name ?
– steenslag
Mar 7 at 17:29
@Shaggon do you want the result to be an array of persons with no duplicate first_name last_name ?
– steenslag
Mar 7 at 17:29
Yes. I want a result with no duplicate first_name last_name and all the item should be belong to array a. The result should be like
[p1]
because p2 have duplicate first_name last_name with p3 and p4 is inside array b, so only p1 is what I want– Shaggon
Mar 8 at 2:26
Yes. I want a result with no duplicate first_name last_name and all the item should be belong to array a. The result should be like
[p1]
because p2 have duplicate first_name last_name with p3 and p4 is inside array b, so only p1 is what I want– Shaggon
Mar 8 at 2:26
Thanks stenfan and steenslag. I found the result I want is
(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:15
Thanks stenfan and steenslag. I found the result I want is
(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:15
add a comment |
2 Answers
2
active
oldest
votes
I'm not sure I get your point, but maybe this is a possible option.
c = a + b
c.uniq! e
Call Array#uniq! with a block on c
which is the concatenation of a
and b
.
1
Thank you so much. I think I found what I want from your comment. It is(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:14
add a comment |
If arrays a and b themselves do not contain people with matching first and last names then this would work:
b.each_with_index do |p, i|
if !(b[i].first_name == a[i].first_name and b[i].last_name == a[i].last_name)
a.push(p) # as people p does not contain the same first/last names as a it can now be added to a
end
end
To check for other fields simply replace first_name / last_name with other variables.
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%2f55045278%2fcombine-arrays-with-conditions-in-ruby%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'm not sure I get your point, but maybe this is a possible option.
c = a + b
c.uniq! e
Call Array#uniq! with a block on c
which is the concatenation of a
and b
.
1
Thank you so much. I think I found what I want from your comment. It is(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:14
add a comment |
I'm not sure I get your point, but maybe this is a possible option.
c = a + b
c.uniq! e
Call Array#uniq! with a block on c
which is the concatenation of a
and b
.
1
Thank you so much. I think I found what I want from your comment. It is(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:14
add a comment |
I'm not sure I get your point, but maybe this is a possible option.
c = a + b
c.uniq! e
Call Array#uniq! with a block on c
which is the concatenation of a
and b
.
I'm not sure I get your point, but maybe this is a possible option.
c = a + b
c.uniq! e
Call Array#uniq! with a block on c
which is the concatenation of a
and b
.
answered Mar 7 at 14:36
iGianiGian
4,6842725
4,6842725
1
Thank you so much. I think I found what I want from your comment. It is(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:14
add a comment |
1
Thank you so much. I think I found what I want from your comment. It is(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:14
1
1
Thank you so much. I think I found what I want from your comment. It is
(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:14
Thank you so much. I think I found what I want from your comment. It is
(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:14
add a comment |
If arrays a and b themselves do not contain people with matching first and last names then this would work:
b.each_with_index do |p, i|
if !(b[i].first_name == a[i].first_name and b[i].last_name == a[i].last_name)
a.push(p) # as people p does not contain the same first/last names as a it can now be added to a
end
end
To check for other fields simply replace first_name / last_name with other variables.
add a comment |
If arrays a and b themselves do not contain people with matching first and last names then this would work:
b.each_with_index do |p, i|
if !(b[i].first_name == a[i].first_name and b[i].last_name == a[i].last_name)
a.push(p) # as people p does not contain the same first/last names as a it can now be added to a
end
end
To check for other fields simply replace first_name / last_name with other variables.
add a comment |
If arrays a and b themselves do not contain people with matching first and last names then this would work:
b.each_with_index do |p, i|
if !(b[i].first_name == a[i].first_name and b[i].last_name == a[i].last_name)
a.push(p) # as people p does not contain the same first/last names as a it can now be added to a
end
end
To check for other fields simply replace first_name / last_name with other variables.
If arrays a and b themselves do not contain people with matching first and last names then this would work:
b.each_with_index do |p, i|
if !(b[i].first_name == a[i].first_name and b[i].last_name == a[i].last_name)
a.push(p) # as people p does not contain the same first/last names as a it can now be added to a
end
end
To check for other fields simply replace first_name / last_name with other variables.
answered Mar 7 at 14:20
Jack BranchJack Branch
373
373
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%2f55045278%2fcombine-arrays-with-conditions-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
2
In your condition you compare
p1
's name top3
's name. What aboutp2
andp4
? Could you give a more complete example with actual data and show the expected output?– Stefan
Mar 7 at 13:52
@Shaggon do you want the result to be an array of persons with no duplicate first_name last_name ?
– steenslag
Mar 7 at 17:29
Yes. I want a result with no duplicate first_name last_name and all the item should be belong to array a. The result should be like
[p1]
because p2 have duplicate first_name last_name with p3 and p4 is inside array b, so only p1 is what I want– Shaggon
Mar 8 at 2:26
Thanks stenfan and steenslag. I found the result I want is
(b + a) - (b + a).uniq e
– Shaggon
Mar 8 at 5:15