سؤال

I'm not even sure how to search if this is an existing questions. Let me just give an example:

Call Instance   Date Created           Resource    Resource Status
------------------------------------------------------------------
6557            2013-07-12 11:34:19    cwood       Accepted
6556            2013-07-12 11:34:18    cwood       Accepted
                2013-07-12 11:29:25    cwood       Ready
6555            2013-07-12 09:24:41    cwood       Accepted

How do I get the top two Accepted entries off the top without getting the last Accepted entry (as it came before a Ready entry)?

All the fields except for Date Created are user-defined classes (Call, User [no, not Resource], and ResourceStatus).

Please let me know if further code would be useful.

هل كانت مفيدة؟

المحلول

You can use takeWhile():

​assert [1,2] == [1,2,3,4,5].takeWhile { it < 3 }​

EDIT(Audacity of @dmahapatro to edit answer)
Small test case for available statuses other than Accepted and Ready:

def list = ['A', 'A', 'B', 'A', 'R', 'A']
assert list.takeWhile{ it == 'A'} == ['A', 'A']
assert list.takeWhile{ it != 'R'} == ['A', 'A', 'B', 'A']

نصائح أخرى

Logic for the below problem statement:

Oh, that's a good point @dmahapatro. Occasionally a user may go Busy while working on a call, and I'll want to get any Accepted statuses that occur before the Busy but after the Ready ("before" being "to the right of"), AND not get the Busy either. So, I suppose I'll need to do statusDescription != 'Ready' and statusDescription != 'Busy' or so. Code sure would be a lot cleaner without people having to use it!

def list = ['A', 'A', 'A', 'R', 'A', 'A','B', 'A']

def statusBeforeBusy = list.takeWhile{ it != 'B'}
println "Statuses before Busy: $statusBeforeBusy" //[A, A, A, R, A, A]

def statusBeforeReady = list.takeWhile{ it != 'R'}
println "Statuses before Ready: $statusBeforeReady" //[A, A, A]

def statusaAfterReadyBeforeBusy = statusBeforeBusy.dropWhile{ it != 'R'}.tail()
println "Statuses After Ready before Busy: $statusaAfterReadyBeforeBusy" //[A, A]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top