How to Store Arrays created on the Fly in Dictionaries

It occurred to me that this might be useful for someone just starting out in coding… if it is too basic (no pun intended) then this is not for you! :wink:

If anyone has a better way or sees an issue please post it!

BTW this will be using API 1 code as I know that off the top of my head … if interested, conversion to API 2 code is an exercise you can undertake if you want to.

I have come across situations where I want to collect a number of objects in a dictionary under a single key in a loop but don’t initially have an array of those objects and don’t know how many different keys I will have.

For someone just starting out, how to handle that may not be obvious as you can’t use the NEW operator to create an Array, as it not considered an object in Xojo.

Consider this code (I am leaving out some parts just to show the principle. I assume the TextStream has been created and is ready to be read and will be closed)

Dim theLine As String, TextStream as TextInput, theKey as String 
Dim DataArray() as DataClass 
Dim theDictionary as New Dictionary

While Not TextStream.EOF 
     theLine = TextStream.Readline
     theKey = ParseKey(theLine)
   
     If theDictionary.HasKey(theKey) Then
         DataArray = theDictionary.value(theKey)
     Else
         Dim NewDataArray()  as DataClass      ' Serves the same purpose New does for objects
         theDictionary.value(theKey) = NewDataArray
         DataArray = NewDataArray
      End if

      DataArray.Append New DataClass(theLine)
Wend

So each key value has its own independent array of DataObjects.

BTW I could have written it this way:

Dim theLine As String, TextStream as TextInput, theKey as String 
Dim theDictionary as New Dictionary

While Not TextStream.EOF 
     theLine = TextStream.Readline
     theKey = ParseKey(theLine)

     Dim DataArray() As DataClass ' Creates a New DataClass Array each time through the loop

     If theDictionary.HasKey(theKey) Then
         DataArray = theDictionary.value(theKey)
     Else
         theDictionary.value(theKey) = DataArray
      End if

      DataArray.Append New DataClass(theLine)
Wend

That gives the same result as the first code but uses one less variable and less code

The reason i do not do it that way is that it creates a new array each time through the loop, but if the key is already in the dictionary, I am just throwing it away, and I don’t like to waste!

-Karen

FWIW you dont actually need to create a new class and can use an array of some simple type in much the same way

Example code is left as an exercise for the reader :slight_smile:

[quote=468949:@Norman Palardy]FWIW you dont actually need to create a new class and can use an array of some simple type in much the same way

Example code is left as an exercise for the reader :)[/quote]

True

I wanted to contrast New And DIM usage, which is why I used a class array instead of a string or double array for the example. Most of the time I do that it is with simple types

-karen

it took a while until i understand what you are doing there :slight_smile:
i think the origin is the saved file format.

[quote=468955:@Markus Rauch]it took a while until i understand what you are doing there :slight_smile:
i think the origin is the saved file format.[/quote]

I just used as text file as an example… the data could be coming from a Database or some external source. The point was how to create new arrays in a loop. Loading data into a dictionary is just one case that I’ve used a few times over the years.

But I hope I am not insulting anyone by posting that because the point is too obvious… it is just something I thought new coders might not initially realize as it is something one rarely needs to do for arrays, except when working with variants like that.

BTW another point was to remind people that putting teh DIM is one ID statement limits it’s scope as well as insure no memory is set aside for it unless needed…

I guess I thought people might get confused because long ago I used to DIM all my variables at the top of a method so it might not occur to someone to do it this way… and IIRC, at one time in the early days REALBasic required that and one was not able to scope variables in a block within a method as we can now…

-Karen

your example is a interesting technique and some kind of sorting.
i think its also useful for charts.

I expanded a module originally created by Kevin Ballard that stores arrays of things (all types of integers, single, doubles, arrays of strings and arrays of folderItems) into a dictionary, and can R/W such dicts to to/from XML files.
Very useful for saving all sorts of complex data to XML text files and reading back into a dict.
Happy to share if anyone is interested.
P.

He is the one that created the REAL Insects app, right?

Karen

Kevin wrote a lot of stuff way back when

Interested!

Here you go:

Look at the methods and read the notes, should get you going. I had to make some minor mods so it’s self-contained but should not have broken anything. Uses MBS for some folderitem stuff but you can work around if you don’t have.