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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      50 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      69 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      36 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      97 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      60 views
      0 likes
      Last Post PaulMohn  
      Working...
      X