Integrated Application Platform › Forums › General › counting based on more than two members of an object from inside a loop
- This topic has 4 replies, 2 voices, and was last updated 8 years, 3 months ago by
ajith.
-
AuthorPosts
-
February 10, 2015 at 3:55 am #1148
ajith
ParticipantHi,
1) I need to construct a loop that goes through an object that conatins object. The child objects all have the same members. In the first cycle, the loop needs to check each member of the child objects and get a count of the child objects that have the value true. Based on this, a score is calculated and in the second loop I need to count the number of child objects that have two members (selected by the previous cycle) with value true. These cycles repeat a number of times. Each time, the number of members based on which I need the count increases by 1.
I do not know how to specify how to count based on two members inside the loop. If I was using a query, I could have appended to the “where” of the query. How do I do something equivalent to objects?
The line of code that does the count the first cycle is
ob.pst += ((row[test1] is true) ? 1:0
In the next cycle,in its place I need do something equiavalent to
ob.pst += ((row[test1] is true and row[test2] is true) ? 1:0
2) Is that possible? If not, is there a way to convert an object of objects to a database table other than outputting one record by one from inside a loop?Thanks,
ajithFebruary 13, 2015 at 7:39 am #1150amckinlay
KeymasterI am not sure exactly what you are looking for.
Your lines of code look valid (except you have extra opening parenthesis)
You could use the “functional” methods like object.Filter and object.Count or the combined object.CountIf
e.g. n = rows.CountIf({ it[test1] is true and it[test2] is true })
There is no shortcut to output a list of objects to a database table. It would be easy to write something that used a loop. If you have the data in memory it will be faster to process it there.
I hope that helps.
February 13, 2015 at 11:53 am #1151ajith
ParticipantHi Andrew,
Thanks for the reply. It appears that I did not state my problem clearly:
The two (and more) lines of codes needs to be generated inside a loop.
The first cycle should generate the first line of code, the second cycle of the loop should generate the second. In the next loop the line of code generated should count based on three columns and so on.If I write manually, the code will work (of course, after ensuring the correct pair of brackets and braces). I can’t write manually, it needs to be generated inside the loop as the number of loops is variable each time I use the code.
The other way that I can think of is to output the objects into a database table and build the “where” query.
Is it possible the first way? Or is there another way?
Thanks,
ajithFebruary 14, 2015 at 8:29 am #1152amckinlay
KeymasterYou could build the code the same way you would build the query where and then use string.Eval. You would need to write it as a function and pass in the arguments. But that should not be necessary.
If you make a list of the field names then you can just loop through them.
function (row, fields) { for f in fields if row[f] isnt true return false return true }
The first time the list would just have one field, the second time two fields, etc.
Is that more what you are looking for?
February 15, 2015 at 10:49 pm #1153ajith
ParticipantHi,
Thanks for the reply.
Yes, I think what I was looking for was string.Eval().
I will try the way that you have suggested and come back if there is any doubts.
Thanks
ajith -
AuthorPosts
- You must be logged in to reply to this topic.