Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lord Simpson · Aug 21, 2011 at 12:31 AM · listcombinelinqcomparison

Combining Items in a single list by comparison

Hi I have a list of items in which a bunch of them are effectively duplicates. I need to compare each item to every other item to see if the they are a match on the correct fields e.g. for a Transform (although my items are not MonoBehaviours)do 2 transforms share the same name, scale and rotation [ignoring position etc] I've tried reverse looping but it seams to miss many items and cant seam to find another way to do it without causing an infinite loop here's what I'm sort of using at the moment

         for (int i = Items.Count - 1; i > 0; i--)
         {
             for (int j = Items.Count - 1; j > 0; j--)
             {
                 if (Items[i].A == Items[j].A && Items[i].B == Items[j].B && Items[i].C == Items[j].D)
                 {
                     Item b1 = Items[i];
                     Item b2 = Items[j];
                     Item b = new Item ( b1.A,b1.B,b1.C,b1.D+b2.D);
                     Items.Insert (0, b);
                     Items.Remove (b1);
                     Items.Remove (b2);
                     i--;
                     j--;
                 }
             }
         }



I've been searching for a solution and seam to find a lot of things mentioning "LinQ" but until a few hours ago id never heard of it and the tutorials I've found don't seam to be have any knowledge I can easily rip out to make this work. Also just to note this wont be running to often [maybe 1-2 times in extreme cases] so shouldn't be to much of a problem if the solution is resource heavy. any help would be greatly appreciated.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sharat · Aug 21, 2011 at 12:39 AM 1
Share

I think the last paragraph of your question got swallowed up by the code section.

avatar image Lord Simpson · Aug 21, 2011 at 12:41 AM 0
Share

Thanks should be readable now :D

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sharat · Aug 21, 2011 at 12:45 AM

UPDATE This ended up being more in depth than I planned, but here's how you can use two lists to do the combination as you have set up.

 List<Object> toRemove;
 List<Object> toAdd;
 
 for(int i = 0; i < originalList.Count; ++i)
 {
     Object obj1 = originalList[i];
     if (toRemove.Contains(obj1))
         continue;
     bool beenAdded = false;
     Object newVal;
     for(int j = i + 1; j < originalList.Count; ++j)
     {
         Object obj2 = originalList[j];
         if (ShouldCombine(obj1, obj2))
         {
             toRemove.AddUnique(obj2);
             if (!beenAdded)
             {
                 toRemove.AddUnique(obj1);
                 newVal = Combine(obj1, obj2);
             }
             else
                 newVal = Combine(newVal, obj2);
             beenAdded = true;
         }
     }
     if (beenAdded)
         toAdd.Add(newVal);
 }
 
 foreach(Object removedObj in toRemove)
     originalList.Remove(removedObj);
 foreach(Object newObj in toAdd)
     originalList.Add(newObj);
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lord Simpson · Aug 21, 2011 at 12:53 AM 0
Share

Ah didn't think of this...only problem is not sure how i could get it to work when I have more than 2 items that require combining [e.g. a,b,c need to be combined so I combine a&b into d but then combine d&c into the final combined item] other than looping over the whole thing with a while loop that only ends when there are no more combinations but that seams extremely brute force and could make things go extremely slow when I may have a large number of items to process.

avatar image Waz · Aug 21, 2011 at 12:53 AM 0
Share

Trouble is, he's not just removing them, he's combining them (eg. adding their quantities), replacing both with a new summed value.

avatar image sharat · Aug 21, 2011 at 12:56 AM 0
Share

I see, I didn't notice that you were combining entries. In that case, I still think the easiest solution is to keep two extra lists, one for all the elements you're going to remove and another for the new elements you will add. I'll update my post.

avatar image sharat · Aug 21, 2011 at 01:25 AM 0
Share

Okay, update's up, ended up having to write more specific protocode than I planned for.

avatar image Lord Simpson · Aug 21, 2011 at 01:28 PM 0
Share

Worked out perfectly Thank you VERY much :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Create a component list from other list 0 Answers

What is the quickest way of getting the most common item in a list or array? 1 Answer

How to combine Vector3 arrays? 3 Answers

C# ArrayList match to string? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges