Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with Dictionary/Lists & Print()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help with Dictionary/Lists & Print()


    Hello I'm pretty new still to Ninjascript and C#. I have a couple of questions regarding the use of Dictionaries. I made a Dictionary to store Bar Numbers, each Key has a List as a Value;

    Dictionary<int, List<int>> BarList = new Dictionary<int, List<int>>();

    After creating a key. Each key is later updated like this:

    BarList[Counter] = new List<int>{Signal1, Signal2, Signal3, Signal4};

    Question1: I could not get AddOrUpdate to work at all with Dictionaries, so every time I want to store a value I have to create a new List. Is AddOrUpdate not supported?

    ========

    Question2: I'm Printing the output like this:

    foreach (KeyValuePair <int, List<int>> Entry in BarList)
    {
    Print (string.Format(@"Key: {0}", Entry.Key));

    foreach (var item in Entry.Value)
    {
    Print (string.Format(@"Bar Number: {0}", item));
    }
    }​

    ​This Outputs this:

    Key: 129
    Bar Number: 3174
    Bar Number: 3175
    Bar Number: 3176
    Bar Number: 3177

    How would I go about Combing the Print outputs into 1 Line? like so:

    Key 129: Bar numbers: 3174, 3175, 3176, 3177​​

    #2
    Hello several,

    With a List inside a dictionary you would have to create a new list every time you add a new item to the dictionary, that list needs to be a valid object so it can later be accessed. Once you add the list you can access it using the key .

    BarList[Counter][0] // first element

    To update the list you would have to use the lists methods by accessing it in the same way

    BarList[Counter].Add(someItem);

    To make the prints appear in 1 line you would need to make a string variable outside the loop and then append each string to the variable and print after the loop completes.


    Code:
    string myString = "";
    foreach (KeyValuePair <int, List<int>> Entry in BarList)
    {
    Print (string.Format(@"Key: {0}", Entry.Key));
    
    foreach (var item in Entry.Value)
    {
    
    myString += string.Format(@"Bar Number: {0}", item) + " ";
    }
    }​​
    Print (myString);

    Comment


      #3
      Thanks for answer. I meant actually each key on it's own line with it's associated values, but I think maybe can work out how to do from what you wrote.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      607 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      353 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      560 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      561 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X