Combining lists in a certain way.

Updated 3 days ago in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
17 on August 3, 2019

I have two lists, taken from an SQLite database. One list has members of a team. The other has their equipment. I want to combine them so that:

ST      Spikehawk Thunderfist

ST1   Bowie knife

ST2   AK47A rifle

PS     Paul Splatter

PS1   9mm Colt 45

PS2    Bayonette

Etc.

What is the best way to do this? I’m using Unity3d 2018 mono 4 equivalent.

Thanks in advance.

  • Liked by
Reply
16 on August 5, 2019

Can you elaborate more on how the 2 list are originally sorted?

Are the first 2 items in the equipment list ALWAYS the 2 items for the first player on the player list?

Are they actually lists, or are they arrays? Could they be stacks or ques?

on August 5, 2019

The schema:

ID and Name for the first list and ID and Equipment for the second.

Currently they are unsorted, just as entered. I’m using a function to pull the data from the database and make it into each list. The data is of string type saved in an SQLite database. I used list because it seemed easy to work with, but if you know a way using a different type, I’m open. I hope this answers your question…

Wise
on August 5, 2019

So the name list has the ID as the [0] element, then the player name for the [1] element, then the 2nd players id for the [2] and the 2nd player’s name for the [3] element?

And the equipment follows the same pattern?

 

so the player list might look like: name and ids random placeholders

[0] 354

[1] Mouledoux

[2] 098

[3] Gilead

[4] 960

[5] Tim

 

and equipment like

[0] 420

[1] Gun

[2] 789

[3] Bigger Gun

[4] 002

[5] Tim’s special gun

 

 

And you want the output list to be

[0] Mouledoux

[1] Gun

[2] Bigger Gun

[3] Gilead

[4] Tim’s special gun

[5]…

 

Is this correct?

on August 5, 2019

Not exactly. I wanted something more like what I posted having a “header” with the name and underneath, that person’s equipment. Does that help or does it mudden the understanding?

 

Wise
on August 5, 2019

What does “header” mean?

How do you want the information formatted, not displayed. You display the information however you want to later. I just want to know what the 2 list look like when you pull them from the DB, and how you want the list to look when you merge them together.

on August 6, 2019

The second list is unsorted and however it was entered:

ST1   Bowie knife

ST2   AK47A rifle

PS1   9mm Colt 45

PS2    Bayonette

The merged list would have the name then equipment, name then equipment.

Looks like what I need is some sort of multidimensional collection where I have             {name:{eq1, eq2, eq3};}

{name:{eq1,eq2,eq3};}

My syntax is poor, I realize, but this is new for me.

Wise
on August 6, 2019
while(nameList.Count > 0 && equipList.Count > 1)
{
    newList.Add(nameList[0]);
    nameList.RemoveAt(0);
 
    for(int i = 0; i < 2; i++)
    {
         newList.Add(equipList[0]);
         equipList.RemoveAt(0);
    }
}
on August 7, 2019

Thanks! What would you think of using a jagged array?

 

Wise
on August 7, 2019

In this case, it would probably be better to just use a class.

Then you could have an array or a list of the class instances. For your player/ equipment example, it might look like this:

 

public class Player
{
     public string name;
     public string[] equipment;
     public Player(string aName, string[] aEquipment)
     {
          name = aName;
          equipment = aEquipment;
     }
}

 

then in that loop we made earlier, instead of combining them into a new list, you could do it like:

while(nameList.Count > 0 && equipList.Count > 1)
{
     string[] equip = new string[] {equipList[0], equipList[1]};
     Player newPlayer = new Player(nameList[0], equip);
 
     nameList.RemoveAt(0);
 
     for(int i = 0; i < 2; i++)
          equipList.RemoveAt(0);
 
     playerList.Add(newPlayer)
}
on August 8, 2019

I was wondering how I would create a class with lists or arrays since the compiler couldn’t convert one class to another and had no idea how to make the constructor for it. I’ll give it a go and let you know. Thanks!

 

6 days ago

I’ve been looking at this wrong al this time. I was trying to combine things before saving them. What I should do is a nested loop to instantiate the members and equipment. Yet even that isn’t working as expected. I’m getting an Argument out of range error.

The result only shows the first name on the list and all the equipment.

  List<Equipment> equipList = new List<BandEquipment> ();
List<Members> nameList = new List<BandMembers> ();
        equipList = DatabaseManager.Instance.MakeEquipmentList ();
        nameList = DatabaseManager.Instance.MakeMembersList ();
        for(int i=0; i<=nameList.Count; i++) 
        {
            GameObject meObj= Instantiate(MemberPrefab);
            Members tmpMember = nameList[i];
       meObj.GetComponent<MemberScript>().DisplayMembers(tmpMember.MemberID, tmpMember.MemberName);
            meObj.transform.SetParent(DisplayparentGig); 
            for (int j=0; j<=equipList.Count; j++) 
            {
                GameObject eqObj= Instantiate(tEquipmentPrefab);
                Equipment tmpEquipment = equipList[j];
                eqObj.GetComponent<EquipmentScript>().DisplayEquipment(tmpEquipment.EquipmentID, tmpEquipment.EquipmentDescription);
                eqObj.transform.SetParent(DisplayparentGig); 
            }
        }

 

 

Show more replies
  • Liked by
Reply
Cancel