Not logged inOpenClonk Forum
Up Topic Development / Scenario & Object Development / Iterate over proplist
- - By Caesar [jp] Date 2015-03-31 05:32
I feel a bit stupid right now, but…

Is there anyway to iterate over all values (or keys) of a proplist? for(var p in ps) doesn't seem to work…

In case you tell me that there is nothing, I will implement it. (I'm still thinking whether I want to provide an interface to loop over the keys. for((var k, var p) in ps) and for(var k in keys ps) are floating in my head. Also, whether that syntax should work on arrays…)
Parent - - By Anonymous [de] Date 2015-03-31 08:32
There is a function that returns all key names I think.
Reply
Parent - - By Zapper [de] Date 2015-03-31 12:51
GetKeys or something like that. I am not at home so I can't check atm :)
Parent - - By Caesar [jp] Date 2015-04-02 04:54 Edited 2015-04-02 07:02
GetProperties. Found it by grepping over C4Script.cpp for functions that use C4PropList for anything else but _this.
But I'll still implement for(… in …) for proplists.

[Edit:]
GetProperties is a very interesting function anyway.

-> GetProperties({a:true, Prototype: {a: false}})
= ["a", "a"]
Parent - - By Sven2 Date 2015-04-02 10:23

> GetProperties is a very interesting function anyway.


Well that's obviously a bug.

> But I'll still implement for(… in …) for proplists.


Do you think that's necessery? To me it seems like it would be only marginally shorter than just using GetProperties(), while still making the scripting language more complex. E.g.:

func DupProplist(p)
{
  var dup = {};
  for (var key in GetProperties(list))
    dup[key] = p[key];
  return dup;
}

func DupProplist2(p)
{
  var dup = {};
  for (var key,value in list)
    dup[key] = value;
  return dup;
}
Parent - - By Caesar [jp] Date 2015-04-02 10:26

>Do you think that's necessery?


The real benefit is that you can use one function for arrays and proplists.

I'm thinking that I may be too lazy to do so though. Turns out it's not exactly easy (as oposed to fixing GetProperties).
Parent - - By Zapper [de] Date 2015-04-02 10:42

>The real benefit is that you can use one function for arrays and proplists.


Might be good from an "we want a very sleek programming language"-point of view.

But I doubt it has ANY benefits for a complete beginner in C4Script OR actually for us experienced developers.
The beginner would just be confused that in arrays you'd iterate over the contents and in proplists over the keys or whatever. I can't really see the benefits :)
Parent - - By Caesar [jp] Date 2015-04-02 10:57
It'd be contents on both of them, obviously.
And even if I'm not doing it with for, I'd still like an efficient method to iterate over a proplist.
Parent - By Zapper [de] Date 2015-04-02 12:26
What would be the usecase for iterating solely over the contents of a proplist?
Usually the key name carries important context information there (unlike in arrays)
Parent - By Sven2 Date 2015-04-02 12:28

> Turns out it's not exactly easy (as oposed to fixing GetProperties).


Why? You'd just check if the type is proplist, and if so, create a temporary array and use the same iteration code that's used for arrays now.

It would be more complicated if you tried it without the temporary copy though.
Parent - By Zapper [de] Date 2015-04-03 17:42
The simplest fix would be using C4PropList::begin() and C4PropList::end() and constructing the list with that iterator
Up Topic Development / Scenario & Object Development / Iterate over proplist

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill