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?










1















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










share|improve this question



















  • 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












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















1















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










share|improve this question



















  • 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












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













1












1








1








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 2:49







Shaggon

















asked Mar 7 at 13:43









ShaggonShaggon

84




84







  • 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












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





    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











  • 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












2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer


















  • 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



















0














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.






share|improve this answer






















    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%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









    1














    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.






    share|improve this answer


















    • 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














    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.






    share|improve this answer


















    • 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








    1







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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













    • 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














    0














    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.






    share|improve this answer



























      0














      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.






      share|improve this answer

























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 14:20









        Jack BranchJack Branch

        373




        373



























            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%2f55045278%2fcombine-arrays-with-conditions-in-ruby%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

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme