this.master_proplist =
{
    a =
    {
        x = 1,
    },
    b =
    {
        x = 4,
    },
    c =
    {
        not_x = 3,
    },
    deeper =
    {
        d =
        {
             x = 2,
        },
    },
}
In this case I would like to get something like:
sorted_array = [master_proplist.a, master_proplist.deeper.d, master_proplist.b]So, when I want to add something to the proplist with the lowest x, I could do:
sorted_array[0].additional_stuff = 42The result would be:
this.master_proplist =
{
    a =
    {
        x = 1,
        additional_stuff = 42,
    },
    b =
    {
        x = 4,
    },
    c =
    {
        not_x = 3,
    },
    deeper =
    {
        d =
        {
             x = 2,
        },
    },
}
Unfortunately, I could not come up with a solution for this. I have tried with GetProperties(), but I could not get into the deeper levels of the proplist. I also get back only an array of strings... Is it even possible to get a result in the form of a pointer, like
master_proplist.a, to use at a later point in the script?Or is there any other solution for setting a value in a sub-proplist that contains a specific key?
func FindProplistsWithProperty(proplist p, string key)
{
  var result = [];
  for (var k in GetProperties(p))
  {
    var v = p[k];
    if (k == key)
      result[GetLength(result)] = p;
    if (GetType(v) == C4V_PropList)
      result[GetLength(result):] = FindProplistsWithProperty(v, key);
  }
  return result;
}
Combined with
SortArrayByProperty(), I think it does what you want:
func Main() {
  var master_proplist =
  {
    a =
    {
      x = 1,
    },
    b =
    {
      x = 4,
    },
    c =
    {
      not_x = 3,
    },
    deeper =
    {
      d =
      {
        x = 2,
      },
    },
  };
  var sorted_array = FindProplistsWithProperty(master_proplist, "x");
  SortArrayByProperty(sorted_array, "x");
  Log("%v", sorted_array); // result: [{x = 1}, {x = 2}, {x = 4}]
  sorted_array[0].additional_stuff = 42;
  Log("%v", master_proplist); // result: {a = {additional_stuff = 42, x = 1}, b = {x = 4}, c = {not_x = 3}, deeper = {d = {x = 2}}}
}
global func PyritsSecretProplistSorter(proplist list, string key_to_sort_by)
{
  var sorted_array = [];
  // Get all properties of the list
  var properties = GetProperties(list);
  // Cycle through the proplist and check for sub-proplists
  for (var property in properties)
  {
    if (GetType(list[property]) != C4V_PropList)
      continue;
    // Push to array if the required property is present
    if (list[property][key_to_sort_by] != nil)
      PushBack(sorted_array, list[property]);
    // Do extremely dangerous recursion call
    var to_add = PyritsSecretProplistSorter(list[property], key_to_sort_by);
    if (to_add != nil && GetLength(to_add) > 0)
      sorted_array = Concatenate(sorted_array, to_add);
  }
  // And now we can sort everything!
  SortArrayByProperty(sorted_array, key_to_sort_by);
  return sorted_array;
}
global func TestPyrit()
{
  var weird_proplist = {
    a =
    {
      x = 1,
    },
    b =
    {
      x = 4,
    },
    c =
    {
      not_x = 3,
    },
    deeper =
    {
      d =
      {
         x = 2,
      },
    }
  };
  var awesome_result = PyritsSecretProplistSorter(weird_proplist, "x");
  Log("%v", weird_proplist);
  Log("%v", awesome_result);
  // Additional stuff
  awesome_result[1].additional_stuff = 42;
  Log("%v", weird_proplist);
}{a = {x = 1}, b = {x = 4}, c = {not_x = 3}, deeper = {d = {x = 2}}}
[{x = 1}, {x = 2}, {x = 4}]
{a = {x = 1}, b = {x = 4}, c = {not_x = 3}, deeper = {d = {additional_stuff = 42, x = 2}}}
-> Scenario::TestPyrit()
Now benchmark them against each other.
[Edit:] Both do crash with cyclic proplists, right?
[Edit:] Both do crash with cyclic proplists, right?
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill

